dounao4179 2015-04-07 15:58
浏览 114
已采纳

PHP无法将数据输入数据库表

I am trying to enter the data that I get from the two variables stuname and book in the table's username and book columns !! I only want to enter data into those two columns since the id column is auto increment and the date is auto updated with time stamp!!! Each time I run my code I enter my data into the two text fields and when I press submit I get this message!!

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\assignment.php on line 35

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\assignment.php on line 36

Here is my Code:

<?php

$servername = "localhost";
$Username = "root";
$Password = "admin";
$Dbname = "nfc";
$conn = mysqli_connect($servername, $Username, $Password, $Dbname);
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "Connected successfully";
if(isset($_POST["stuname"])&&($_POST["book"]))
{
$stuname =  $_POST["stuname"];
$book =$_POST["bookname"]; 
$sql = "INSERT INTO library (id, username, book, date)
VALUES ('', '$stuname', '$book','')";

       mysqli_select_db($conn, 'nfc') or die(mysqli_error($con));
       $retval = mysqli_query( $sql, $conn );
       if(! $retval )
        {
            die('Could not enter data: ' . mysql_error());
        }


        else 
        {
            echo "Success";
        }

        echo " to stuname ". $stuname;
        echo " to book ". $book;
}
?>
<form id="form1" name="form1" method="post" action="#">
  <p>
    <label for="1">student name</label>
    <input type="text" name="stuname" id="1"  />
  </p>
  <p>
    <label for="12">book name</label>
    <input type="text" name="bookname" id="12" />
  </p>
  <input name="submit" type="submit" value="Submit" />

</form>
  • 写回答

2条回答 默认 最新

  • dongtangyi8962 2015-04-07 16:11
    关注

    In the mysqli_query you should put the conn first and then the query itself

    $retval = mysqli_query( $conn, $sql );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码