doujie2356
2014-04-14 08:58
浏览 30
已采纳

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

I am trying to insert data into a database mysql phpAdmin.

My webhost is 000webhost.

My connection to mysql database code:

    <?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');


?>

My insert data code:

 <?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>

Instead of letting me put it into the database it brings me to this page http://error404.000webhost.com/?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doujiongqin0687 2014-04-17 04:58
    已采纳

    It DID have to be mysqli

    my problem was the - had to be changed to an _ on <form method="post" action="insert-data.php">

    working code:

    <?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>
    
    点赞 打赏 评论
  • dongyaofu0599 2014-04-14 09:03

    First change below code:

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

    To :

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

    Then change below code:

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

    To:

    $sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
    
    点赞 打赏 评论
  • drxm72811 2014-04-14 09:04

    try change

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

    to

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

    and also insert query and connection to

    $dbcon = mysql_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
    
    $sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
    
    点赞 打赏 评论
  • douyue7408 2014-04-14 09:09

    can u please change this line?

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

    to

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

    you are setting wrong attributes so its not getting the values..

    after that, u have to update your sql query:

    $sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
    
    点赞 打赏 评论

相关推荐 更多相似问题