2013-03-03 82 views
3

你好我是PHP新手,我使用xampp作为本地主机和html表单来收集数据。PHP处理表单数据

我的表格要求太平洋数据,我试图让它把这些数据转化为MySQL数据库客户端

我得到错误警告:请求mysql_query()预计参数2是资源,对象在C中给出: \ XAMPP \ htdocs中\ service_record.php线14上 错误:

形式代码

<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代码

<?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); 

?> 

我已阅读书籍后,它改变了100次书籍仍然没有结果。 我已阅读本网站和许多其他人,我只是可以得到它。

如果可以,请帮助。

罗布

+2

您的脚本似乎很容易受到SQL注入攻击。 – Gumbo 2013-03-03 14:27:13

回答

3

您使用mysqli_connect,但后来,你检查!mysql_query,并关闭它mysql_close

因此,您必须决定是否要使用mysqlmysqli。首先,要尝试机制,到处使用mysql,它应该很好。但是,请将所有内容重写为mysqli,这是更新更好的(!)。

When should I use MySQLi instead of MySQL?

MySQL vs MySQLi when using PHP

希望它可以帮助你。

+4

从不使用'mysql' – JamesHalsall 2013-03-03 14:26:36

+0

谢谢Stepo,第一次工作,我现在只需将MySQL更改为mysqli – pitbull10 2013-03-03 14:59:47

+0

不客气。是的,只需将旧的mysql更改为新的mysqli即可。如果你发现我的答案有用,你可以接受它:)) – Stepo 2013-03-03 15:14:02