Jump to content

Recommended Posts

Hi

Firstly I am new to programming so go easy on me :) 

 

I am building a page where the user inputs text into a table called 'products'. This is the code I am using:

	$productname=$_POST['productname'];
	$productprice=$_POST['productprice'];
	$productpostage=$_POST['productposage'];
	$productquick=$_POST['productquick'];
	$productdescription=$_POST['productdescription'];
	$productdelivery=$_POST['productdelivery'];
	mysql_connect('localhost', 'username', 'password') or die(mysql_error());
	mysql_select_db('Kanga') or die(mysql_error());
	mysql_query("INSERT INTO 'product' VALUES ('$productname', '$productprice', '$productpostage', '$productquick', '$productdescription', '$productdelivery')");
		print "$productname has been added to the database.";
?>

When I press the submit button on the form, I get "Hello world(this is what i entered into the productname field) has been added to the database."

 

I am using phpmyadmin, so should all the data that is submitted show up in there? 

 

Thanks

 

Jarrod

You'll get that message regardless of whether the query works. You need to check the return value of mysql_query(): it will return true for success and false for failure.

But that only checks whether the query successfully ran - not whether the query inserted any records. Use mysql_affected_rows for that.

 

You're starting off, right? Don't use the mysql extension and mysql_* functions. They're old and slow and not as good as the alternatives: PDO or mysqli. Look into both and decide which you'd rather use (the overall style is similar but there are a few differences here and there).

You'll get that message regardless of whether the query works. You need to check the return value of mysql_query(): it will return true for success and false for failure.

But that only checks whether the query successfully ran - not whether the query inserted any records. Use mysql_affected_rows for that.

 

You're starting off, right? Don't use the mysql extension and mysql_* functions. They're old and slow and not as good as the alternatives: PDO or mysqli. Look into both and decide which you'd rather use (the overall style is similar but there are a few differences here and there).

 

If I use MYSQLi, do I just add the 'I' after the MYSQL? 

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.