Jump to content

Recommended Posts

I can't seem to get variables to work with my query statement. Everything works fine if I manually input what I want in the PHP code, but not from the variables. I have added various echo statements just to make sure that all the variables actually hold something, and they do. I have also run some select statements to make sure that the database is actually connected and it is. I get no error message from this, it just leaves a blank screen. Here is the code.

[code]
<html>
<head>
    <title>Book Entry Results</title>
</head>
<body>
<h1>Book Entry Results</h1>
<?php
    //create short variable names
    $isbn=$_POST['isbn'];
    $author=$_POST['author'];
    $title=$_POST['title'];
    $price=$_POST['price'];
    
    if(!$isbn || !$author || !$title || !$price)
    {
        echo "You have not entered all the required details.<br />"
            ."Please go back and try again.";
        exit();
    }
    
    if(!get_magic_quotes_gpc())
    {
        $isbn=addslashes($isbn);
        $author=addslashes($author);
        $title=addslashes($title);
        $price=doubleval($price);
    }
    
    $db = new mysqli('localhost', 'username', 'password', 'books');
    
    if (mysqli_connect_errno())
    {
      echo "Error: Could not connect to database.  Please try again later.";
      exit();
    }
    $query = "INSERT INTO books VALUES
            ('$isbn', '$author', '$title', '$price')";
    
    //echo $query;
    
    //$result = $db->query($query);
    $result = mysqli_query($db, $query);
    
    //echo $result;
    
    if($result)
        echo $db->affected_rows.' book inserted into database.';
    else
        echo "This sucks";
        
    $db->close();
    
?>
</body>
</html>
[/code]

Any help would be greatly appreciated.
Link to comment
https://forums.phpfreaks.com/topic/11519-variables-and-mysql-insert/
Share on other sites

The problem I see with your insert statement is the following, it specifies the values to be inserted but not the columns to insert the values to. You need to construct the statement like this:

INSERT INTO tablename (col1, col2, col3) VALUES ('val1', 'val2', 'val3')

hope that helps
[!--quoteo(post=381570:date=Jun 8 2006, 01:05 PM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 8 2006, 01:05 PM) [snapback]381570[/snapback][/div][div class=\'quotemain\'][!--quotec--]
The problem I see with your insert statement is the following, it specifies the values to be inserted but not the columns to insert the values to. You need to construct the statement like this:

INSERT INTO tablename (col1, col2, col3) VALUES ('val1', 'val2', 'val3')

hope that helps
[/quote]


I have actually tried it that way. I have also tried

[code]
$query = "INSERT INTO books
                set isbn = '$isbn',
                author = '$author',
                title = '$title',
                price = '$price'";
[/code]

I have even tried not bothering with the query variable and just plugging the full query into

[code]
$result = mysqli_query($db, "");
[/code]

And no matter how I form it, I still get the blank page, but if I replace the variables with words/numbers the database is updated and the results are shown.


Thanks,
Robert
use the code Crayon Violent suggested and print out any errors, like this

[code]
$query = "INSERT INTO books (isbn,author,title,price) VALUES ('$isbn','$author','$title','$price')";
$result = mysql_query($query) or die(mysql_error());
[/code]

The error message should tell you what's wrong, if you need more help, you can post it here.
It was the mysqli_error($db) function that made me realize I was sending an invalid character. I feel a bit dumb since it was such a small mistake, but I guess it's useful for learning. Thank you all for the help, I spent 3 hours trying to find the answer to this question with Google searches, and all I really had to do was post here.



Thanks,
Robert
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.