Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello!
Bookname or author are not posted in mysql. There is only 0. When I insert number to the form it stores number but not name.

newbook.html:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        fieldset {
            width: 75%;
            border: 2px solid #cccccc;
        }

        label {
            width: 75px;
            float: left;
            text-align: left;
            font-weight: bold;
        }

        input {
            border: 1px solid #000;
            padding: 3px;
        }
    </style>
</head>
<body>
    <h1>Book-O-Rama - Novy zaznam knihy</h1>
    <form action="insert_book.php" method="post">
        <fieldset>
            <p><label for="ISBN">ISBN</label>
            <input type="text" id="ISBN" name="ISBN" maxlength="13" size="13">
            </p><p>
                <label for="Author">Autor</label>
                <input type="text" id="Author" name="Author" maxlength="30" size="30">
            </p><p>
                <label for="Title">Nazov</label>
                <input type="text" id="Title" name="Title" maxlength="60" size="60">
            </p>
        </fieldset>
        <p><input type="submit" value="Pridat novu knihu"></p> 
    </form>
</body>
</html>



insert_book.php
PHP
<pre><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Vysledok vlozenia knihy</h1>
    <?php
    if(empty($_POST['ISBN']) || empty($_POST['Author']) || empty($_POST['Title'])) {
        echo "<p>Nezadali ste vsetky povinne udaje</p><br>Vratte sa prosim spat a skuste to znovu.";
        exit;
    }

    //Vytvarame skratene nazvy premennych
    $isbn = $_POST['ISBN'];
    $author = $_POST['Author'];
    $title = $_POST['Title'];

@$db = new mysqli('localhost','root','','books');

if (mysqli_connect_errno()) {
    echo "<p>Chyba - nepodarilo sa pripojit k databazi</p>";
    exit;
}

$query = "INSERT INTO Books VALUES (?, ?, ?)"; 
$stmt = $db->prepare($query);
$stmt->bind_param('ssd', $isbn, $author, $title);
$stmt->execute();

if ($stmt->affected_rows > 0) {
    echo "<p>Kniha bola uspesne vlozena do databazy.</p>";
} else {
    echo "<p>Nastala chyba!<br>Polozku sa nepodarilo pridat.";
}

$db->close();
?>
</body>
</html>


What I have tried:

Lookup problem on the internet.
Posted
Updated 13-Nov-21 20:51pm
Comments
Luc Pattyn 13-Nov-21 16:22pm    
Two suggestions:

1. echo all three variables ($isbn, $author, $title) right before the bind_param statement.

2. as an experiment, change the order of the fields e.g. swap isbn and author (in field list and in bind_param) and see what that gives,

Looking at the bind statement
PHP
$stmt->bind_param('ssd', $isbn, $author, $title);

You've defined that title is a double. It probably should be defined as string. Like
PHP
$stmt->bind_param('sss', $isbn, $author, $title);

Also note that it's a good practice to define the fields you're inserting into, so instead of
PHP
$query = "INSERT INTO Books VALUES (?, ?, ?)"; 

try
PHP
$query = "INSERT INTO Books (isbn, author title) VALUES (?, ?, ?)"; 

This prevents the values from going to incorrect columns in case the order of the columns is different in the database or for example if columns are added to the table.
 
Share this answer
 
v2
I am beginner and this is code from the book. I dont know what else should I check. When I enter the text in the form and check in phpmyadmin there is 0 in the field. Is this normal? Or is PHPmyadmin broken?
xampp 8.0.x
windows 11
 
Share this answer
 
v2
Comments
Richard MacCutchan 13-Nov-21 8:35am    
This is not a Solution. If you have information to add then please use the Improve question link above, and add complete details of what is not working. But please ensure you add full details, saying "only numbers are working" is not much help to anyone.
Ive created table in PHPmyadmin maybe because of that was stored only numbers there
now I created table via commands and text is showing correctly
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900