输入表单的问题

时间:2014-06-13 10:02:38

标签: php mysql

我创建了一个简单的表单,我希望更新mysql数据库中的一些信息。我通过它看起来很好,但我得到一些错误

我得到了(两个文件):

<?php

$db_name = "test2";
$un = "jharvard";
$pw = "crimson";
$host = "localhost";

$connect = mysql_connect($host, $un, $pw);
mysql_select_db($db_name) or die(mysql_error());

echo ("succesfully conneted to the database!");

?>

<?php

if (isset($_POST['submitted']))

{


//Dit is de php file waarmee je connectie met de database maakt.
include ("addEmployee.php");

    $fname = $_POST['fname'];
    $lname = $_POST['lname'];

    $sqlinsert = "INSERT INTO customers (name, address) VALUES ('$fname', '$lname')";

    if (!mysqli_query($connect, $sqlinsert))

    {
    die ('error!');
    }



}


?>


<html>

Add somebody!

<body>

    <form method= "post" action="oefInsertData.php">

        <input type = "hidden" name ="submitted" value="true" />

        First name: <input type = "text" name="fname"/>
        Last name: <input type = "text" name="lname"/>

        <input type="submit" value="Add new person"/>

    </form>




</body>

如果我运行它我会收到以下错误:! )警告:mysqli_query()期望参数1为mysqli,第16行/home/jharvard/vhosts/pset7/public/oefInsertData.php中给出的资源

有人知道这里出了什么问题吗?

4 个答案:

答案 0 :(得分:0)

替换

<?php

$db_name = "test2";
$un = "jharvard";
$pw = "crimson";
$host = "localhost";

$connect = mysql_connect($host, $un, $pw);
mysql_select_db($db_name) or die(mysql_error());

echo ("succesfully conneted to the database!");

?>

<?php
$con=mysqli_connect("localhost","jharvard","crimson","test2");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

答案 1 :(得分:0)

您已从mysql_切换到mysqli_,因此$connect不是mysqli参数,可以使用mysqli_connect reference

更改此设置

答案 2 :(得分:0)

您同时使用my_sqlmy_sqli分机..

使用此..

$connect = mysqli_connect($host, $un, $pw, $db_name);

并避免

mysql_select_db($db_name) or die(mysql_error());

答案 3 :(得分:0)

    <?php

$db_name = "test2";
$un = "jharvard";
$pw = "crimson";
$host = "localhost";
?>
and
<?php

if (isset($_POST['submitted']))
{
//Dit is de php file waarmee je connectie met de database maakt.
include ("addEmployee.php");
mysql_connect("$host", "$un", "$pw")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$fname = $_POST['fname'];
$lname = $_POST['lname'];

$query = mysql_query("INSERT INTO customers (name, address) VALUES ('{$fname}', '{$lname}')");
if(!$query){ echo mysql_error(); }

}


?>