PHP代码的结果是错误的?

时间:2016-05-28 10:23:32

标签: php

当我尝试制作一个简单的PHP代码时,我遇到了一个问题,结果显示了一些不期望的东西,例如“ '。”\ n“; echo' “。我的代码在哪里错了?

这是我的代码:

   <html>
<head>
<title>Putting Data in the DB</title>
</head>
<body>
<?php
/*insert students into DB*/
if(isset($_POST["submit"])) {
$db = mysql_connect("mysql", "martin");
mysql_select_db("martin");
$date=date("Y-m-d");  /* Get the current date in the right SQL format */
$sql="INSERT INTO students  VALUES(NULL,'" . $_POST["f_name"] . "','" . 
$_POST["l_name"] . "'," . $_POST["student_id"] . ",'" . $_POST["email"] . 
"','" . $date . "'," . $_POST["gr"] . ")";  /* construct the query */
mysql_query($sql);  /* execute the query */ 
mysql_close();
echo"<h3>Thank you. The data has been entered.</h3> \n";
echo'<p><a href="data_in.php">Back to registration</a></p>' . "\n";
echo'<p><a href="data_out.php">View the student lists</a></p>' ."\n";
}
else {
?> 
<h3>Enter your items into the database</h3>
<form action="data_in.php" method="post">
First Name: <input type="text" name="f_name" /> <br/>
Last Name: <input type="text" name="l_name" /> <br/>
ID: <input type="text" name="student_id" /> <br/>
email: <input type="text" name="email" /> <br/>
Group: <select name="gr">
<option value ="1">1</option>
<option value ="2">2</option>
<option value ="3">3</option>
</select><br/><br/>
<input type="submit" name="submit" /> <input type="reset" />
</form>
<?php
} /* end of "else" block */
?>
</body>
</html>

结果在这里:

  

谢谢。数据已输入。 \ n“; echo'返回注册

     

'。 “\ n” 个; echo'查看学生列表

     

'。“\ n”; } else {?&gt;在数据库中输入您的项目

1 个答案:

答案 0 :(得分:0)

正确的代码:

<html>
<head>
    <title>Putting Data in the DB</title>
</head>
<body>
<?php if (isset($_POST["submit"])): ?>
    <?php
        /*insert students into DB*/
        $db = mysql_connect("mysql", "martin");
        mysql_select_db("martin");
        $date = date("Y-m-d");  /* Get the current date in the right SQL format */
        $sql = "INSERT INTO students  VALUES(NULL,'" . $_POST["f_name"] . "','" .
            $_POST["l_name"] . "'," . $_POST["student_id"] . ",'" . $_POST["email"] .
            "','" . $date . "'," . $_POST["gr"] . ")";  /* construct the query */
        mysql_query($sql);  /* execute the query */
        mysql_close();
    ?>
    <h3>Thank you. The data has been entered.</h3>
    <p><a href="data_in.php">Back to registration</a></p>
    <p><a href="data_out.php">View the student lists</a></p>
<?php else: ?>
    <h3>Enter your items into the database</h3>
    <form action="data_in.php" method="post">
        First Name: <input type="text" name="f_name"/> <br/>
        Last Name: <input type="text" name="l_name"/> <br/>
        ID: <input type="text" name="student_id"/> <br/>
        email: <input type="text" name="email"/> <br/>
        Group: <select name="gr">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select><br/><br/>
        <input type="submit" name="submit"/> <input type="reset"/>
    </form>
<?php endif ?>

顺便说一句,请尝试使用PDO
http://www.w3schools.com/php/php_mysql_prepared_statements.asp
否则,有人可以输入"); TRUNCATE table students; --并清除所有数据。

这是经典的 - https://xkcd.com/327/