0

I have a html form that i want to submit its data into a specific database in wamp using phpmyadmin, the connection is successfully done. However, the data cannot be submitted. I get this message after submitting the data in the form :

Successful connection
( ! ) Warning: mysqli_query() expects parameter 1 to be 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 to 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 into 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

Liam Sorsby
  • 2,912
  • 3
  • 28
  • 51
Judy
  • 424
  • 2
  • 4
  • 13

2 Answers2

1

you posted your connection codes in comments (which belongs in your question I might add) being mysql_ based.

You need to use mysqli

those different MySQL APIs do not intermix. You must use the same one from connection to query.

Example pulled from the manual:

<?php 
//conection:
$link = mysqli_connect("myhost","myuser","mypassw","mybd") 
        or die("Error " . mysqli_error($link)); 

and remember to replace $link with $dbconn and your own credentials.

This doesn't help you:

die('Error inserting new records!');

this does:

or die(mysqli_error($dbconn));

Since you seem new to this, use prepared statements right away.

References:

Your present code is open to SQL injection.


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.


Just for argument's sake, put a space between INSERT and INTO:

$sqlinsert= "INSERT INTO `test` (`FName`,`LName`) VALUES ('$fname','$lname')";

You seem to have made a reference to that in comments that they are seperated, but I said it anyway.

  • Plus, try putting your connection/include on top of your conditional statement.

Connection:

Your connection should be this and replacing the xxx with your 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_ at all.

Sidenote: @ symbols are error suppressors. You can add them back in once everything is working.


Closing notes:

Kudos to Liam (Sorsby).

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • I corrected that .. I'm not getting the warning but still error inserting the records, I'm getting this instead : Successful connectionError inserting new records! – Judy Jul 23 '15 at 16:40
  • @Judy you need to find out why your query failed. consult http://php.net/manual/en/mysqli.error.php - add `or die(mysqli_error($dbconn))` to `mysqli_query()` – Funk Forty Niner Jul 23 '15 at 16:42
  • Thank you anyway...but there is no problem with the connection...the problem is about inserting the data . – Judy Jul 23 '15 at 16:47
  • @Judy reload my answer and look at error reporting and why your query is failing under **this does:** – Funk Forty Niner Jul 23 '15 at 16:48
  • Thanks a lot... this is what i wrote and it worked. $dbconn = @mysqli_connect("$db_host","$db_username","$db_pass","$db_name") or die("Error".@mysqli_error($dbconn)); @mysql_select_db("$db_name") or die("No database"); echo"Successful connection"; – Judy Jul 23 '15 at 16:57
  • @Judy you also have ` ` in your question but no related code for it in your SQL. If that is indeed part of it, you need to modify your question/code for it. I tested your code, no problems. Check to see if you haven't a restraint somewhere, that the column names have the right type and lengths. If your inputs have apostrophes, or anything else that MySQL will complain about, then that would be a contributing factor. I see no errors with what I posted as a solution. – Funk Forty Niner Jul 23 '15 at 17:03
  • @Judy the @ symbols in @mysqli_connect etc. **suppresses** errors. and you can't use `@mysql_select_db("$db_name") or die("No database");` - there should be **no** instances of `mysql_` whatsoever. Please go over the manual on connecting, as stated in my answer. – Funk Forty Niner Jul 23 '15 at 17:04
  • @Judy You're welcome Judy. I'm glad to see this was resolved, *cheers* – Funk Forty Niner Jul 23 '15 at 17:10
  • 1
    @Fred-ii- Growing another cactus here :)? – Rizier123 Jul 23 '15 at 17:16
  • 1
    @Rizier123 yes, God love 'em :-) – Funk Forty Niner Jul 23 '15 at 17:17
  • 1
    @Fred-ii- Nice answer! Finally Solved. +1 from me. This is why I don't get rep. I can't be bothered with answers like this. Haha – Liam Sorsby Jul 23 '15 at 17:22
  • 1
    @LiamSorsby Thank you Liam. I made a slight edit. See "Closing notes". *cheers* ;-) – Funk Forty Niner Jul 23 '15 at 17:25
  • Haha! Thank you very much ;-) haha – Liam Sorsby Jul 23 '15 at 17:25
  • @LiamSorsby You're welcome Liam. You were the *crystal ball* on this one ;-) – Funk Forty Niner Jul 23 '15 at 17:27
  • 1
    Now onto the next one. Ha – Liam Sorsby Jul 23 '15 at 17:28
0

Use separated words like,

INSERT INTO `test` (`FName`,`LName`) VALUES ('$fname','$lname')";
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
lingo
  • 1,848
  • 6
  • 28
  • 56