jazza96 Posted May 24, 2015 Share Posted May 24, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/296466-how-do-i-know-data-is-being-inputted-into-the-database/ Share on other sites More sharing options...
requinix Posted May 24, 2015 Share Posted May 24, 2015 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). Quote Link to comment https://forums.phpfreaks.com/topic/296466-how-do-i-know-data-is-being-inputted-into-the-database/#findComment-1512534 Share on other sites More sharing options...
jazza96 Posted May 24, 2015 Author Share Posted May 24, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/296466-how-do-i-know-data-is-being-inputted-into-the-database/#findComment-1512537 Share on other sites More sharing options...
requinix Posted May 24, 2015 Share Posted May 24, 2015 No, they're two different things. First came mysql and later came mysqli ("mysql improved") so they're related, but they're still different. Quote Link to comment https://forums.phpfreaks.com/topic/296466-how-do-i-know-data-is-being-inputted-into-the-database/#findComment-1512540 Share on other sites More sharing options...
CroNiX Posted May 25, 2015 Share Posted May 25, 2015 Just look up the mysqli functions on php.net. They show the proper use. Quote Link to comment https://forums.phpfreaks.com/topic/296466-how-do-i-know-data-is-being-inputted-into-the-database/#findComment-1512613 Share on other sites More sharing options...
maxxd Posted May 27, 2015 Share Posted May 27, 2015 IMO, skip MysqlI and look into PDO - it's easier to use with prepared statements and parameter binding. The queries you've written are wide open to SQL injection. Using prepared statements will mitigate that risk. Quote Link to comment https://forums.phpfreaks.com/topic/296466-how-do-i-know-data-is-being-inputted-into-the-database/#findComment-1512705 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.