3

Hello I am a newbie to PHP, I am using xampp as a local host and html forms to collect data.

my form asks for pacific data, I am trying to get it to put that data into MySQL database client

I am getting error Warning: mysql_query() expects parameter 2 to be resource, object given in C:\xampp\htdocs\service_record.php on line 14 Error:

form code

<center>
    <form method="post" action="service_record.php">
        <table border="1" cellspacing="50">
            <center><h1>Record Service Record</h1></center>
            <tr>
                <td>Date joined  <input name="join_date"size="34" maxlength="30"> </td>
                <td>Discharge date  <input name="leave_date"size="34" maxlength="30"><br /></td>
            </tr>
            <tr>
                <td>Rank <input name="rank"size="20" maxlength="20"> </td>
                <td>time served <input name="time_served"size="20" maxlength="10"></td>
           </tr>
           <tr> 
               <td>Service <input name="service"size="34" maxlength="30"></td> 
               <td>Regt/Corps  <input name="regt_corps"size="34" maxlength="30"><br /></td>
           </tr>
       </table>

       <input type="reset" value="Reset Form"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  <input  type="submit" value="Send" />
</center>
    </form>

php code

<?php
    $con=mysqli_connect("localhost","client","");
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sql="INSERT INTO service_record (join_date,leave_date,rank,time_served,service,regt_corps)
     VALUES
     ('$_POST[join_date]','$_POST[leave_date]','$_POST[rank]','$_POST[time_served]','$_POST[service]','$_POST[regt_corps]')";

    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    echo "The Staff Details have been added to database";

    mysql_close($con);

?>

I have read books after books changed it 100 times still no result. I have read this site and many others, I just can get it.

Please help if you can.

Rob

SilentAssassin
  • 468
  • 1
  • 9
  • 27
pitbull10
  • 21
  • 4

1 Answers1

3

You are using mysqli_connect, but then, you are checking for !mysql_query and you close it as mysql_close.

So, you have to decide if you want to use mysql, or mysqli. At first, to try mechanism, use mysql everywhere, it should work well. But then, please, rewrite all to mysqli, it's newer and better(!).

When should I use MySQLi instead of MySQL?

MySQL vs MySQLi when using PHP

Hope it helps you.

Community
  • 1
  • 1
Stepo
  • 1,036
  • 1
  • 13
  • 24