mysql - Cannot submit a form on php and mysqli_query -
i have html form want submit data specific database in wamp using phpmyadmin, connection done. however, data cannot submitted. message after submitting data in form :
successful connection ( ! ) warning: mysqli_query() expects parameter 1 mysqli, resource given in c:\wamp\www\ex\insert-data.php on line 11 call stack # time memory function location 1 0.0005 136600 {main}( ) ..\insert-data.php:0 2 0.0023 144480 mysqli_query ( ) ..\insert-data.php:11 error inserting new records!
my code in 'insert-data.php' is:
<?php if(isset($_post['submitted'])){ include('connect.php'); $fname = $_post['fname']; $lname = $_post['lname']; $sqlinsert= "insertinto`test`(`fname`,`lname`)values('$fname','$lname')"; if(!mysqli_query($dbconn,$sqlinsert)){ die('error inserting new records!'); } echo "1 record added database"; } ?> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <h1>insert data db</h1> </head> <body> <form method="post" action="insert-data.php" > <input type="hidden" name="submitted" value="true" /> <label>first name</label> <input type="text" name="fname" /> <label>last name</label> <input type="text" name="lname" /> <input type="checkbox" name="check" /> <input type="radio" name="radios" /> <input type="submit" value="submit"></button> </form> </body> </html>
any idea? ....thanks
you posted connection codes in comments (which belongs in question might add) being mysql_
based.
you need use mysqli
those different mysql apis not intermix. must use same 1 connection query.
example pulled manual:
<?php //conection: $link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("error " . mysqli_error($link));
and remember replace $link
$dbconn
, own credentials.
this doesn't you:
die('error inserting new records!');
this does:
or die(mysqli_error($dbconn));
since seem new this, use prepared statements right away.
references:
- http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php
- http://php.net/pdo.prepared-statements
your present code open sql injection.
add error reporting top of file(s) find errors.
<?php error_reporting(e_all); ini_set('display_errors', 1); // rest of code
sidenote: displaying errors should done in staging, , never production.
just argument's sake, put space between insert
, into
:
$sqlinsert= "insert `test` (`fname`,`lname`) values ('$fname','$lname')";
you seem have made reference in comments seperated, said anyway.
- plus, try putting connection/include on top of conditional statement.
connection:
your connection should , replacing xxx
own credentials.
$db_host = "xxx"; $db_username = "xxx"; $db_pass = "xxx"; $db_name = "xxx"; $dbconn = mysqli_connect("$db_host","$db_username","$db_pass","$db_name") or die("error".mysqli_error($dbconn));
and nothing else. no instances of mysql_
@ all.
sidenote: @
symbols error suppressors. can add them in once working.
closing notes:
kudos liam (sorsby).
Comments
Post a Comment