Page 1 of 1

insert not working either

Posted: Tue Nov 15, 2005 12:23 pm
by flann
I'm trying to insert into a mysql database and my insert isn't working. Is there a problem with this code?

Code: Select all

<?php
    
    mysql_connect('localhost', 'username', 'password') or die ("Couldn't connect to database ");
    mysql_select_db('movies') or die("Can't select db ". mysql_error());;
    
    $isbn = $_POST['txtISBN'];
    $title = $_POST['txtTitle'];
    $rating = $_POST['txtRating'];
    $query = "insert into dvds (isbn, title, rating)
        values('$isbn', '$title', '$rating')";

    $result = mysql_query($query) or die("can't create query ". mysql_error());
    if (mysql_affected_rows() == 1) {
        echo '<p>Your information has been recorded</p>';
    } else {
        error_log(mysql_error());
        echo '<p>Something went wrong</p>';
    }
}
?>
The connection is working fine, the values are getting into the variables just fine. The problem is that it isn't inserting into the database and it's also not printing either condition from the "if statement".

Posted: Tue Nov 15, 2005 12:29 pm
by John Cartwright
I split this thread from viewtopic.php?t=40633

Posted: Tue Nov 15, 2005 12:32 pm
by John Cartwright
try doing a print_r($_POST) and see if you are recieving the expected values

if that doesn't reveal anything suspicious then try changing your query to

Code: Select all

$query = "INSERT INTO `dvds` (`isbn`, `title`, `rating`) values('".$isbn."', '".$title."', '".$rating."')";

insert not working

Posted: Tue Nov 15, 2005 12:58 pm
by flann
I'm getting the expected results from the variables. I also tried changing the insert, and that didn't work either. I've made a simple select statement work just fine, it's just this f'n insert.

Re: insert not working

Posted: Tue Nov 15, 2005 1:01 pm
by foobar
flann wrote:I'm getting the expected results from the variables. I also tried changing the insert, and that didn't work either. I've made a simple select statement work just fine, it's just this f'n insert.
Do you have the correct privileges to insert into the database? (Just to make sure ;) )

Posted: Tue Nov 15, 2005 1:03 pm
by twigletmac
If you print out the SQL statement, does it run if you paste it into something like phpMyAdmin or the MySQL command line tool?

Mac

Posted: Tue Nov 15, 2005 1:05 pm
by flann
If you're asking if it works when I do the insert statement in the command line, then yes, my original insert statement worked fine in the command line.

Posted: Tue Nov 15, 2005 1:12 pm
by twigletmac
What do you get if you do:

Code: Select all

echo 'The result from mysql_affected_rows(): '.mysql_affected_rows();
after the query?

Mac

Posted: Tue Nov 15, 2005 1:18 pm
by flann
a blank page

Posted: Tue Nov 15, 2005 1:21 pm
by twigletmac
So you're getting no output at all? Is this code in between other code so that any output might be hidden? Do you have PHP set to display errors in case there's a syntax error or something similar hanging about in the PHP?

Mac

Posted: Tue Nov 15, 2005 1:21 pm
by flann
I did also make sure that in the mysql database and the user table that the user has a 'Y' in the insert_priv field.

Posted: Tue Nov 15, 2005 1:23 pm
by flann
here is the entire code for the page.

Code: Select all

<html>
<head>
<title>Insert Page</title>
</head>
<body>
<p>top</p>
<?php
	
	mysql_connect('localhost', 'username', 'password') or die ("Couldn't connect to database ");
	mysql_select_db('movies') or die("Can't select db ". mysql_error());;
	
	$isbn = $_POST['txtISBN'];
	$title = $_POST['txtTitle'];
	$rating = $_POST['txtRating'];

 $query = "INSERT INTO dvds (isbn, title, rating) values('".$isbn."', '".$title."', '".$rating."')"; 

	$result = mysql_query($query) or die("can't create query ". mysql_error());
	echo 'The result from mysql_affected_rows(): '.mysql_affected_rows();
	if (mysql_affected_rows() == 1) {
		print '<p>Your information has been recorded</p>';
	} else {
		error_log(mysql_error());
		print '<p>Something went wrong</p>';
	}
}
?>
<p>bottom</p>
</body>
</html>

Posted: Tue Nov 15, 2005 1:27 pm
by flann
I found the problem, I had an extra }. Thank you for helping me work through this problem. The answer was in the error log :)

thanks again.

Posted: Tue Nov 15, 2005 1:32 pm
by twigletmac
Cool - if this is your development machine it would probably be an idea to turn display_errors on in your php.ini so that it's a bit easier to see when things are wonky :)

Mac

Posted: Tue Nov 15, 2005 2:38 pm
by foobar
Jcart wrote:*removed by the all mighty power*
Oo! Oo! I am gonna teh haxx0r j00 with my leet session jacking skills!! :P