joshstevens19 Posted April 25, 2013 Share Posted April 25, 2013 so im connecting my netbeans php to phpmyadmin (database).. now in this i am trying to create a php code statement which will take the inserted data into the html fields and insert them into my database successful.. anybody know what im doing wrong here is the error message: Warning: mysql_query() expects parameter 1 to be string, resource given in C:\xampp\htdocs\PhpProject1\New Review.php on line 27 error inserting new record <?php if (isset($_REQUEST['submitted'])) { include('connect database.php'); $showtitle = $_POST['SHOWTITLE']; $showreview =$_POST['SHOWREVIEW']; $sqlinsert = "INSERT INTO reviews (SHOWTITLE,SHOWREVIEW) VALUES('$showtitle', '$showreview')"; if (!mysql_query($dbConn,$sqlinsert)) { die('error inserting new record'); } here is my html coding with it <form method="post" action="New Review.php"> <input type="hidden" name="submitted" value="true" /> <fieldset> <legend> New Reviews</legend> <label> Enter show title:<input type ="text" name="SHOWTITLE" /></label> <label> Enter Review: <input type ="text" name="SHOWREVIEW" /></label> </fieldset> <br /> <input type ="submit" value ="add new review" /> </form> Quote MultiQuote Quote Link to comment Share on other sites More sharing options...
seandisanti Posted April 25, 2013 Share Posted April 25, 2013 That error typically means that your query has failed and the failure has not been handled gracefully. echo out your $sqlinsert and I think you'll see the issue. Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 25, 2013 Share Posted April 25, 2013 The mysql_query() function goes mysql_query($string, $connection). It seems you have it backwards. Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted April 25, 2013 Author Share Posted April 25, 2013 when i echo it out it comes up with this Warning: mysql_query() expects parameter 1 to be string, resource given in C:\xampp\htdocs\PhpProject1\New Review.php on line 27error inserting new record and when i switch them about all it says is 'error inserting new record' but no warning codes.. whats the next thing i could do as it not inserting it into the database which i am trying to achieve Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted April 25, 2013 Author Share Posted April 25, 2013 The mysql_query() function goes mysql_query($string, $connection). It seems you have it backwards. That error typically means that your query has failed and the failure has not been handled gracefully. echo out your $sqlinsert and I think you'll see the issue. when i echo it out it comes up with this Warning: mysql_query() expects parameter 1 to be string, resource given in C:\xampp\htdocs\PhpProject1\New Review.php on line 27 error inserting new record and when i switch them about all it says is 'error inserting new record' but no warning codes.. whats the next thing i could do as it not inserting it into the database which i am trying to achieve Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 25, 2013 Share Posted April 25, 2013 You actually have to query the database... $query = mysql_query($sqlinsert, $dbConn); if (!$query) { die('error inserting new record'); } ALSO, close your if bracket. You close your if isset, but not your if not query. Quote Link to comment Share on other sites More sharing options...
seandisanti Posted April 25, 2013 Share Posted April 25, 2013 change this line die('error inserting new record'); to be die('error inserting new record<br />' . mysql_error()); Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted April 25, 2013 Author Share Posted April 25, 2013 You actually have to query the database... $query = mysql_query($sqlinsert, $dbConn); if (!$query) { die('error inserting new record'); } ALSO, close your if bracket. You close your if isset, but not your if not query. change this line die('error inserting new record'); to be die('error inserting new record<br />' . mysql_error()); <?php if (isset($_POST['submitted'])) { include('connect database.php'); $showtitle = $_POST['SHOWTITLE']; $showreview =$_POST['SHOWREVIEW']; $sqlinsert =mysql_query ("INSERT INTO reviews (SHOWTITLE,SHOWREVIEW) VALUES('$showtitle', '$showreview'"); if (!mysql_query($sqlinsert, $dbConn)) { die('error inserting new record</b>'. mysql_error); } thats is my code for the insert statment... now the query is in that.. here what it brings up Notice: Use of undefined constant mysql_error - assumed 'mysql_error' in C:\xampp\htdocs\PhpProject1\New Review.php on line 28 error inserting new recordmysql_error line 27 is die('error inserting new record</b>'. mysql_error); now what am i doing wrong.. my SQL code maybe that is wrong but their is something in this code which wont allow it to work... pleasee help me guys haaa i have been dying with this code for ages if you know can you put it in my code for me so i dont get confused like a example etc? thanks for your help Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 25, 2013 Share Posted April 25, 2013 mysql_error() not mysql_error. Add the parentheses Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted April 25, 2013 Author Share Posted April 25, 2013 mysql_error() not mysql_error. Add the parentheses im new to php can you expand... i have changed to mysql_error()); all i want to do is get it to insert and it is not working can you look at my code and suggest a way round it? Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 25, 2013 Share Posted April 25, 2013 Ok, I can help explain this to you. So you have a query variable $sqlinsert, which is good. You if not statement is wrong though. It should something like this. $sqlinsert = "INSERT INTO reviews (`SHOWTITLE`, `SHOWREVIEW`) VALUES('$showtitle', '$showreview')"; $query = mysql_query($sqlinsert); if (!query) { echo "error inserting new record" . mysql_error(); } I also added some backticks `` to your query for your column names. If there are any errors in your query now it will tell you. Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted April 25, 2013 Author Share Posted April 25, 2013 Ok, I can help explain this to you. So you have a query variable $sqlinsert, which is good. You if not statement is wrong though. It should something like this. $sqlinsert = "INSERT INTO reviews (`SHOWTITLE`, `SHOWREVIEW`) VALUES('$showtitle', '$showreview')"; $query = mysql_query($sqlinsert); if (!query) { echo "error inserting new record" . mysql_error(); } I also added some backticks `` to your query for your column names. If there are any errors in your query now it will tell you. i changed my code to this <?php $showtitle = $_POST['SHOWTITLE']; $showreview =$_POST['SHOWREVIEW']; $sqlinsert = ("INSERT INTO reviews (`SHOWTITLE`, `SHOWREVIEW`) VALUES('$showtitle', '$showreview'"); $query =mysql_query($sqlinsert); if (!$query) { echo "error inserting new record" . mysql_error(); } now when i run it comes up with this error ?? Notice: Undefined index: SHOWTITLE in C:\xampp\htdocs\PhpProject1\New Review.php on line 23 Notice: Undefined index: SHOWREVIEW in C:\xampp\htdocs\PhpProject1\New Review.php on line 24 error inserting new recordYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 please help me to get this sorted? Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 25, 2013 Share Posted April 25, 2013 Are you column names SHOWTITLE and SHOWREVIEW?? In the database. Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted April 25, 2013 Author Share Posted April 25, 2013 Are you column names SHOWTITLE and SHOWREVIEW?? In the database. yes they are.. thats what they are called .. not sounding cheeky but could you write a code for inserting into a database with my fields or something to help me out and i try that? .. i just dont get what i am doing wrong (((((((((((((( thanks a lot for your help Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted April 25, 2013 Author Share Posted April 25, 2013 Are you column names SHOWTITLE and SHOWREVIEW?? In the database. do you think it could be this?? if (isset($_POST['submitted'])) { include('connect database.php'); $showtitle = $_POST['SHOWTITLE']; $showreview =$_POST['SHOWREVIEW']; $sqlinsert =mysql_query ("INSERT INTO `reviews` (SHOWTITLE,SHOWREVIEW) VALUES(['$showtitle'], ['$showreview']"); if (!mysql_query($sqlinsert)) { die('error inserting new record'); at the bottom it says the if statment but it only says die not if it works etc??? do i need a result key or anything to finilise it Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 25, 2013 Share Posted April 25, 2013 Ooo lol! It means that those two values don't have anything in them, so it can't insert anything. Put something in the input. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 25, 2013 Share Posted April 25, 2013 the errors are referring to the two $_POST variables. the form you posted apparently isn't the one that is submitting to that code and those two fields don't exist or are invalid html in the form. Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted April 25, 2013 Author Share Posted April 25, 2013 Ooo lol! It means that those two values don't have anything in them, so it can't insert anything. Put something in the input. the errors are referring to the two $_POST variables. the form you posted apparently isn't the one that is submitting to that code and those two fields don't exist or are invalid html in the form. okay so here is my code: <?php if (isset($_POST['submitted'])) { include('connect database.php'); $showtitle = $_POST['SHOWTITLE']; $showreview =$_POST['SHOWREVIEW']; $sqlinsert =mysql_query ("INSERT INTO `reviews` (SHOWTITLE,SHOWREVIEW) VALUES('$showtitle', '$showreview'"); if (!mysql_query($sqlinsert)) { die('error inserting new record'); } what changes would you make to it ?? im very terrible at this lol !!.. the names on the database on SHOWTITLE and SHOWREVIEW Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 25, 2013 Share Posted April 25, 2013 You are missing the point. It doesnt have to do with the code right now. The form that you are submitting the information from either doesn't have any information in it or it is the wrong form. Put information in the form and hit submit. Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted April 25, 2013 Author Share Posted April 25, 2013 You are missing the point. It doesnt have to do with the code right now. The form that you are submitting the information from either doesn't have any information in it or it is the wrong form. Put information in the form and hit submit. the database form has information it .. it is a review page.. all i want it to do is when someone rites in the field it inserts into the database.. simple as that.. i dont know why it is doing it when there is information in the database and all im asking of it is to just place it in the slots i have i issued within the code i.e SHOWTITLE ETC.. ??? Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 25, 2013 Share Posted April 25, 2013 Are you still getting the index error? The index error means that there is nothing in the query to insert. Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted April 25, 2013 Author Share Posted April 25, 2013 Are you still getting the index error? The index error means that there is nothing in the query to insert. no errors at all.. only thing i get is 'error inserting new record' which as you can see is my If statments output if (!mysql_query($sqlinsert)) { die('error inserting new record'); so i may be rite by saying the code is doing it but it will only ever die because thats the only way it can go.. there is no else clauise or anything the if statment is saying even if successful Die?? am i correct? Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 25, 2013 Share Posted April 25, 2013 Well while I look at it again, did you close this if statement like I told you to before?? if (!mysql_query($sqlinsert)) { die('error inserting new record'); } You need that closing bracket on this one Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 25, 2013 Share Posted April 25, 2013 at least one of the versions of the code is running mysql_query() on the actual query, then running mysql_query() again on the result resource from the first mysql_query(). your original code didn't have the first mysql_query() statement and you randomly added it at some point for no reason. Quote Link to comment Share on other sites More sharing options...
joshstevens19 Posted April 25, 2013 Author Share Posted April 25, 2013 at least one of the versions of the code is running mysql_query() on the actual query, then running mysql_query() again on the result resource from the first mysql_query(). your original code didn't have the first mysql_query() statement and you randomly added it at some point for no reason. Well while I look at it again, did you close this if statement like I told you to before?? if (!mysql_query($sqlinsert)) { die('error inserting new record'); } You need that closing bracket on this one yes the bracket is done on it.. okay i understand a little bit mac but i still dont have a clue how to fix it as soon as i can fix this im laughing! lolll sorry i sound so dumb Quote Link to comment 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.