[英]insert values from various drop down list to database table
我是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 ['roll'],$ _ post ['first_exam']這些錯誤。
那里只有一個簡單的錯誤。 取而代之的是,您制作了許多表格,只需制作一張表格並將所有選擇添加到其中...
像這樣:
<form method="post"> code here </form>
您的代碼中有很多錯誤,這是一個完整的工作示例:
HTMl和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個form標記即可通過POST
或GET
方法發送多個字段。 INSERT * INTO
放進去, 星號僅用於SELECT
語句) </body>
, </html>
標簽 or die (mysql_error());
你把它針對MySQL的execute
或connect
功能,而不是針對字符串。 嘗試這個。 填寫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());
這也是一個問題。 使用or die (mysql_error());
在mysql函數中,而不是在聲明中。
<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>
我想指出的幾件事。
這是完整的代碼,
<?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);
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.