我是PHP新手。我没有面向对象PHP的经验。我只有Raw PHP的经验。
我正在尝试将下拉框的值插入数据库。这是我在这里给出的代码。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
if (isset($_POST['submit']))
{
$roll=$_POST['roll'];
$first_exam=$_POST['first_exam'];
$second_exam=$_POST['second_exam'];
$third_exam=$_POST['third_exam'];
$fourth_exam=$_POST['fourth_exam'];
$fifth_exam=$_POST['fifth_exam'];
include 'db.php';
$sql= "INSERT * INTO roll (id,Roll,1st,2nd,3rd,4th,5th)
VALUES (NULL,$roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)"
or die (mysql_error());
$result= mysql_query($sql);
if ($result){
echo 'ok doen';
}
else {
echo 'dont';
}
}
?>
</body>
<form method="post">
<tr>
<td>Roll:</td>
<td><input type="text" name="roll" /></td>
</tr>
<br></br>
</form>
<form method="post">
<?php echo "first_exam";?>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<?php echo "second_exam";?>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<?php echo "third_exam";?>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<?php echo "fourth_exam";?>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post" action="">
<?php echo "fifth_exam";?>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</form>
<br></br>
<form method="post">
<input type="submit" name="submit" value="submit">
</form>
<br><br>
</form>
</html>
我收到了$ _post [&#39; roll&#39;],$ _ post [&#39; first_exam&#39;]这些错误。
答案 0 :(得分:1)
那里只有一个简单的错误。相反,你做了很多,只做一个表格和将所有选择添加到其中......
这样的事情:
<form method="post">
code here
</form>
答案 1 :(得分:1)
你的代码中有很多错误,这是一个完整的例子:
HTMl&amp; PHP:<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Divix Help</title>
</head>
<body>
<?php
if (isset($_POST['submit']))
{
$roll=$_POST['roll'];
$first_exam=$_POST['first_exam'];
$second_exam=$_POST['second_exam'];
$third_exam=$_POST['third_exam'];
$fourth_exam=$_POST['fourth_exam'];
$fifth_exam=$_POST['fifth_exam'];
include 'db.php';
$sql= "
INSERT INTO roll (id,Roll,1st,2nd,3rd,4th,5th)
VALUES (NULL,$roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)
";
//echo $sql;
$result = mysql_query($sql);
if ($result){
echo 'ok doen';
} else {
echo 'dont';
}
}
?>
<form method="post">
<table>
<tr>
<td>Roll:</td>
<td><input type="text" name="roll" /></td>
</tr>
</table>
<br></br>
<?php echo "first_exam";?>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "second_exam";?>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "third_exam";?>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "fourth_exam";?>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<?php echo "fifth_exam";?>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
<br></br>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
<form>
个代码,请记住,您只需要1个表单代码,以便通过POST
或GET
方法发送多个字段。 INSERT * INTO
,星号符号仅放入SELECT
语句中</body>
,</html>
标记or die (mysql_error());
你把它放在mysql的execute
或connect
函数上而不是字符串。答案 2 :(得分:0)
试试这个。制作1表格。
也改变这一点。
$sql= "INSERT INTO roll (Roll,1st,2nd,3rd,4th,5th) VALUES ($roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)";
$result= mysql_query($sql) or die (mysql_error());
这也是一个问题。在mysql函数中使用or die (mysql_error());
,而不是在语句中使用。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form method="post">
<table>
<tr>
<th>Roll:</th>
<th>First Exam</th>
<th>Second Exam</th>
<th>Third Exam</th>
<th>Fourth Exam</th>
<th>Fifth Exam</th>
<th>Action</th>
</tr>
<tr>
<td><input type="text" name="roll" /></td>
<td>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
<td><input type='submit' name='submit' value='submit'></td>
</tr>
</table>
</form>
<?php
if (isset($_POST['submit']))
{
$roll=$_POST['roll'];
$first_exam=$_POST['first_exam'];
$second_exam=$_POST['second_exam'];
$third_exam=$_POST['third_exam'];
$fourth_exam=$_POST['fourth_exam'];
$fifth_exam=$_POST['fifth_exam'];
include 'db.php';
$sql= "INSERT INTO roll (id,Roll,1st,2nd,3rd,4th,5th) VALUES (NULL,$roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)";
$result= mysql_query($sql) or die (mysql_error());
if ($result){
echo 'ok doen';
}
else {
echo 'dont';
}
}
?>
</body>
</html>
答案 3 :(得分:0)
我想指出的事情很少。
这是完整的代码,
<?php
// instead of include, use require_once
require_once('db.php');
// create and use Database class for all your database operations
class Database{
public static function open_connection($host, $username, $password, $dbName){
return new mysqli($host, $username, $password, $dbName);
}
public static function check_connection(){
if(!mysqli_connect_error()){
return true;
}else{
return false;
}
}
public static function execute_query($connection,$query){
return $connection->query($query);
}
public static function close_connection($connection){
$connection->close();
}
}
?>
<?php
if (isset($_POST['submit'])){
$connection = Database::open_connection(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
if(Database::check_connection()){
// sanitize your input data
$roll = $connection->real_escape_string($_POST['roll']);
$fifth_exam = $connection->real_escape_string($_POST['first_exam']);
$second_exam = $connection->real_escape_string($_POST['second_exam']);
$third_exam = $connection->real_escape_string($_POST['third_exam']);
$fourth_exam = $connection->real_escape_string($_POST['fourth_exam']);
$fifth_exam = $connection->real_escape_string($_POST['fifth_exam']);
// now insert into database
// instead of taking id as NULL, take id as AUTO_INCREMENT
$query = "INSERT INTO table(roll_no, first_exam, second_exam, third_exam, fourth_exam, fifth_exam) VALUES ($roll, '$first_exam', '$second_exam', '$third_exam', '$fourth_exam', '$fifth_exam')";
if(Database::execute_query($connection,$query)){
echo "record is successfully inserted";
}else{
echo "error: record could not be inserted";
}
}else{
echo "database connection failed";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Some Title</title>
</head>
<body>
<form action="test.php" method="post">
<table>
<tr>
<td>Roll No.</td>
<td><input type="text" name="roll" /></td>
</tr>
<tr>
<td>First exam</td>
<td>
<select name="first_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Second exam</td>
<td>
<select name="second_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Third exam</td>
<td>
<select name="third_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Fourth exam</td>
<td>
<select name="fourth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td>Fifth exam</td>
<td>
<select name="fifth_exam">
<option value="math">Mathematics</option>
<option value="phy">Physics</option>
<option value="chem">Chemistry</option>
<option value="eng">English</option>
<option value="bio">Biology</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
<?php
// it's a good programming practice to always close database connection at the end
if(isset($connection)){
Database::close_connection($connection);
}
?>