MattD00 Posted January 15, 2011 Share Posted January 15, 2011 I have created a php insert to add data input from a html form into a phpmyadmin database runnig in xxamp. php connect file works fine <?php $connect = mysql_connect("localhost","root","") or die("Connection error"); mysql_select_db("ria") or die(mysql_error()); ?> php code in insert data into database not working <?php if (isset($_POST['required'])) { include ('connection.php'); $title = $_POST['title']; $cost = $_POST['cost']; $stock = $_POST['no in stock']; $insert = "INSERT INTO book (title, cost, no in stock) VALUES ('$title', '$cost', '$stock')"; if (!mysqli_query($connect, $insert)) { die('can not insert new record'); } $newrecord = "New record added"; } ?> HTML forms <form action="Insert.php"> <input type="hidden" name="required" value="true" /> <fieldset> <legend>Insert New Book</legend> <label>Title <input type="text" name="title" /></label> <label>Cost <input type="text" name="cost" /></label> <label>No. in Stock <input type="text" name="no in stock" /> </label> </fieldset> <br /> <input type="submit" value="Submit" /> </form> last bit of php to give a message when data has been added <?php echo $newrecord ?> any suggestions what is wrong with the php. Link to comment https://forums.phpfreaks.com/topic/224543-mysql-insert-error/ Share on other sites More sharing options...
Pikachu2000 Posted January 15, 2011 Share Posted January 15, 2011 Assuming you're getting the 'can not insert new record' error, change it so errors are reported, and the query string is echoed. <?php if (isset($_POST['required'])) { include ('connection.php'); $title = $_POST['title']; $cost = $_POST['cost']; $stock = $_POST['no in stock']; $insert = "INSERT INTO book (title, cost, no in stock) VALUES ('$title', '$cost', '$stock')"; if ( !mysqli_query($connect, $insert) ) { die( "<br>Query string: $insert<br>Failed with error: " . mysqli_error($connect) ); } else { if( mysqli_affected_rows($connect) > 0 ) { $newrecord = "New record added"; } } } ?> [/code] Link to comment https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1159886 Share on other sites More sharing options...
mikosiko Posted January 15, 2011 Share Posted January 15, 2011 INSERT INTO book (title, cost, no in stock) is not the error obvious? Link to comment https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1159991 Share on other sites More sharing options...
Pikachu2000 Posted January 16, 2011 Share Posted January 16, 2011 Well, it is to me. I was just hoping to help someone learn to debug Link to comment https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1160005 Share on other sites More sharing options...
mikosiko Posted January 16, 2011 Share Posted January 16, 2011 @pikachu I know that you know Link to comment https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1160008 Share on other sites More sharing options...
Pikachu2000 Posted January 16, 2011 Share Posted January 16, 2011 LOL. Link to comment https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1160019 Share on other sites More sharing options...
Muddy_Funster Posted January 16, 2011 Share Posted January 16, 2011 Once the problem with the PHP is sorted, your then going to get a problem with the actual SQL aswell....Unless everything that you are entering into the database is a number. @Pikachu - Loving that cartoon in your sig! Link to comment https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1160328 Share on other sites More sharing options...
MattD00 Posted January 18, 2011 Author Share Posted January 18, 2011 I couldn't get any of it to work because i forgot to add method="post" to the html form. Now that I have the php script will run and i get a Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in F:\xampp\htdocs\RIA\insert.php on line 18 Warning: mysqli_error() expects parameter 1 to be mysqli, resource given in F:\xampp\htdocs\RIA\insert.php on line 19 I tried googling the error but still not sure how to fix it. Link to comment https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1161540 Share on other sites More sharing options...
PFMaBiSmAd Posted January 18, 2011 Share Posted January 18, 2011 You are using mysql_ functions in your connection code and mysqli_ functions in your main code. You cannot mix mysql_ and mysqli_ on the same connection. Link to comment https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1161543 Share on other sites More sharing options...
MattD00 Posted January 18, 2011 Author Share Posted January 18, 2011 I have change the mysqli to mysql and the error is still the same. Link to comment https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1161546 Share on other sites More sharing options...
PFMaBiSmAd Posted January 18, 2011 Share Posted January 18, 2011 Define: change the mysqli to mysql? You cannot just change the name because the parameter list is different. If the error message is exactly the same, then you didn't save the change or you didn't put the new file onto your server. Link to comment https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1161549 Share on other sites More sharing options...
MattD00 Posted January 18, 2011 Author Share Posted January 18, 2011 Well i saved the file into xxamps htdocs folder. <?php if (isset($_POST['required'])) { include ('connection.php'); $title = $_POST['title']; $cost = $_POST['cost']; $stock = $_POST['no_in_stock']; $insert = "INSERT INTO book (title, cost, no_in_stock) VALUES ('$title', '$cost', '$stock')"; if ( !mysql_query($connect, $insert) ) { die( "<br>Query string: $insert<br>Failed with error: " . mysql_error($connect) ); } else { if( mysql_affected_rows($connect) > 0 ) { $newrecord = "New record added"; } } } ?> I just got rid of the i on mysqli to make it mysql not sure if that was right or not. Link to comment https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1161556 Share on other sites More sharing options...
mikosiko Posted January 18, 2011 Share Posted January 18, 2011 mysql_query: http://php.net/manual/en/function.mysql-query.php mysqli_query: http://php.net/manual/en/mysqli.query.php different formats Link to comment https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1161558 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.