为什么我的插入数据代码不起作用?

时间:2014-04-14 08:58:58

标签: php html mysql phpmyadmin

我正在尝试将数据插入数据库mysql phpAdmin。

我的网站主机是000webhost。

我与mysql数据库代码的连接:

    <?PHP

$mysql_host = "mysql2.000webhost.com";
$mysql_database = "*********";
$mysql_user = "********";
$mysql_password = "**********";

$dbcon = mysql_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);


if (!$dbcon) {
    die('error connecting to database');
    }
echo ('You have connected successfully');


?>

我的插入数据代码:

 <?PHP

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

    include('connect_mysql.php');

    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";

    if (!mysql_query($dbcon, $sqlinsert)) {
        die('error inserting new record');
        } // end of nested if statement
        $newrecord = "1 record added to the database";
}

?>

<html>
<head>
<title>Insert Data into DB</title>
</head>
<body>

<h1>Insert Data into DB</h1>

<form method="post" action="insert-data.php">
<input type="hidden" name="submitted" value="true" />
<fieldset>
    <legend>New People</legend>
    <label>First Name: <input type="text" name="fname" /></label>
    <label>Last Name: <input type="text" name="lname" /></label>
</fieldset>
<br />
<input type="submit" value="add new person" />
</form>
<?PHP
echo $newrecord
?>

</body>
</html>

不是让我把它放到数据库中,而是将它带到了这个页面 http://error404.000webhost.com/

4 个答案:

答案 0 :(得分:1)

尝试更改

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

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

并插入查询和连接

$dbcon = mysql_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);

$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";

答案 1 :(得分:0)

首先更改以下代码:

$dbcon = mysqli_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);

致:

$dbcon = mysql_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);

然后更改以下代码:

 $sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('fname', 'lname')";

要:

$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";

答案 2 :(得分:0)

你能改变这条线吗?

<fieldset>
    <legend>New People</legend>
    <label>First Name: <input type="text name="fname" /></label>
    <label>Last Name: <input type="text name="lname" /></label>
</fieldset>

<fieldset>
    <legend>New People</legend>
    <label>First Name: <input type="text" name="fname" /></label>
    <label>Last Name: <input type="text" name="lname" /></label>
</fieldset>

你设置了错误的属性,所以它没有得到值..

之后,你必须更新你的SQL查询:

$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";

答案 3 :(得分:0)

它必须是mysqli

我的问题是 - 必须在<form method="post" action="insert-data.php">

上更改为_

工作代码:

<?PHP

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

    include('connect_mysql.php');
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";

    if (!mysqli_query($dbcon, $sqlinsert)) {
    die('error inserting new record');
    }
    $newrecord = "1 new record added to the database";
}





?>

<html>
<head>
<title>Insert Data into DB</title>
</head>
<body>

<h1>Insert Data into DB</h1>

<form method="post" action="insert_data.php">
<input type="hidden" name="submitted" value="true" />
<fieldset>
    <legend>New People</legend>
    <label>First Name: <input type="text" name="fname" /></label>
    <label>Last Name: <input type="text" name="lname" /></label>
</fieldset>
<br />
<input type="submit" value="add new person" />
</form>
<?PHP
echo $newrecord
?>
</body>
</html>