Insert Problems

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
flann
Forum Commoner
Posts: 38
Joined: Tue Aug 23, 2005 10:48 pm

insert not working either

Post 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".
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I split this thread from viewtopic.php?t=40633
Last edited by John Cartwright on Tue Nov 15, 2005 2:58 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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."')";
flann
Forum Commoner
Posts: 38
Joined: Tue Aug 23, 2005 10:48 pm

insert not working

Post 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.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Re: insert not working

Post 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 ;) )
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
flann
Forum Commoner
Posts: 38
Joined: Tue Aug 23, 2005 10:48 pm

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
flann
Forum Commoner
Posts: 38
Joined: Tue Aug 23, 2005 10:48 pm

Post by flann »

a blank page
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
flann
Forum Commoner
Posts: 38
Joined: Tue Aug 23, 2005 10:48 pm

Post 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.
flann
Forum Commoner
Posts: 38
Joined: Tue Aug 23, 2005 10:48 pm

Post 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>
flann
Forum Commoner
Posts: 38
Joined: Tue Aug 23, 2005 10:48 pm

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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
Post Reply