dongyi6845 2012-03-11 07:44
浏览 22
已采纳

将数据插入数据库时​​出现问题

My code doesn't insert any records to mysql. What is wrong? I am really confused. I have designed a form and I want to read data from text box and send to the database.

<?php
if(isset($_post["tfname"]))
    {
        $id=$_post["tfid"];
        $name=$_post["tfname"];
        $family=$_post["tffamily"];
        $mark=$_post["tfmark"];
        $tel=$_post["tftell"];

$link=mysql_connect("localhost","root","");
if (!$link)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("university",$link);
$insert="insert into student (sid,sname,sfamily,smark,stel) values ($id,'$name','$family',$mark,$tel)";

mysql_query($insert,$link);
    }
mysql_close($link);
?>
  • 写回答

4条回答 默认 最新

  • duanjiao8871 2012-03-11 07:57
    关注

    You'd better to put quotation mark for id, mark and tel after values in your query. Also as @Another Code said, you must use $_POST instead of $_post in your code. Try this and tell me the result:

    <?php
    if(isset($_POST["tfname"])) {
       $id=$_POST["tfid"];
       $name=$_POST["tfname"];
       $family=$_POST["tffamily"];
       $mark=$_POST["tfmark"];
       $tel=$_POST["tftell"];
    
       $link=mysql_connect("localhost","root","");
       if (!$link) {
          die('Could not connect: ' . mysql_error());
       } else {
          mysql_select_db("university",$link);
          $insert="insert into student 
                   (sid,sname,sfamily,smark,stel) values 
                   ('$id','$name','$family','$mark','$tel')";
          mysql_query($insert,$link) or die (mysql_error());
          mysql_close($link);
       }
    } else {
       die('tfname did not send');
    }
    ?>
    

    Use mysql_query($insert,$link) or die (mysql_error()); to fetch the error message.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?