douzi1991 2015-01-28 18:27
浏览 10
已采纳

无法在mySQL表中插入

I am trying to make a registration form. I can connect successfully to mySQL database but i can't insert anything to my table named "hackers" from my html form. Here is my form and my php script.

I also tried to insert data manually in $sql to see if the problem is in the form, but $sql doesn't add data either.

Your help is much much appreciated!!!!!!!

<?php
include"config.php";

$insert = 'INSERT INTO hackers (name,email, school, grad-year)  VALUES('$_POST['name']','$_POST['email']','$_POST['school']','$_POST['gradYear']')';


$sql= 'INSERT INTO mydatabase_name.hackers (name,email, school, grad-year) VALUES ('text', 'text', 'text', 'text')';

mysql_query($insert);
mysql_query($sql);


?>

My form:

<form method = "post" action ="process.php" >
    <table id ="registration_form">
    <tr>
        <td>name</td>
        <td><input type= "text" name="name"/></td>
    </tr>
     <tr>
        <td>email</td>
        <td><input type= "text" name="email"/></td>
    </tr>
    <tr>
        <td>school</td>
        <td><input type= "text" name="school"/></td>
    </tr>
         <tr>
        <td>graduation year</td>
        <td><input type= "text" name="gradYear"/></td>
    </tr>

    <tr>
        <td><input type= "submit" name="submit" value ="Submit"/></td>
    </tr>
   </table>
</form>
  • 写回答

3条回答 默认 最新

  • dongyi1939 2015-01-28 18:31
    关注

    You need to concatenate with ., use double quotes on the outside and single values for value adding. For example:

    "INSERT INTO hackers (name,email, school, grad-year)  VALUES('".$_POST['name']."','".$_POST['email']."','".$_POST['school']."','".$_POST['gradYear']."')"
    

    Although I want to note that this isn't a good idea as you are prone to SQL-injections. You want to escape or prepare the query first before you execute it.

    For your manually entered string you have a similar problem. In this case an easy fix is to have double quotes on the outside of the string and single quotes on the inside, the problem is that this ES ('te closes the string for the query here: $sql= 'INS.

    "INSERT INTO mydatabase_name.hackers (name,email, school, grad-year) VALUES ('text', 'text', 'text', 'text')"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能