本文介绍了如何使用PHP将select选项保存到数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到错误说:
错误:INSERT INTO dbstation(From,To,Date)值('fAG','tkulim','2018-09-30')你有错误你的SQL语法;检查与您的MariaDB服务器版本对应的手册,以便在第1行的From,To,Date值('fAG','tkulim','2018-09-30')附近使用正确的语法
你能帮我解决这个问题吗?提前谢谢。
这是我的HTML代码:
< html>
< head>
< title>主菜单< / title>
< link rel =stylesheettype =text / csshref =menu_style.css>
< link rel =stylesheettype =text / csshref =Datepicker.css>
< / head>
< body>
< div class =menu>
<! - < div class =btn> - >
< a href =main.html>< button class =btn>预订< / button>< / a>
< a href =history.html>< button class =btn>历史< / button>< / a>
< a href =reservation.html>< button class =btn>预订< / button>< / a>
< a href =trainpay.html>< button class =btn> TrainPay< / button>< / a>
< a href =reservation.html>< button class =btn>更新个人资料< / button>< / a>
< br>< br>< br>
<! - < / div> - >
<! - < div class =station> - >
< form align =centermethod =postaction =station.php>
来自
< select name =from>
< option value =fkluang> Kluang,Johor< / option>
< option value =fAG> Alor Gajah,Melaka< / option>
< option value =fSenawang> Senawang,NS< / option>
< option value =fKL> KL Sentral,KL< / option>
< option value =fipoh> Ipoh,Perak< / option>
< option value =fkulim> Kulim,Kedah< / option>
< option value =fkangar> Kangar,Perlis< / option>
< / select>
到
< select name =to>
< option value =tkluang> Kluang,Johor< / option>
< option value =tAG> Alor Gajah,Melaka< / option>
< option value =tSenawang> Senawang,NS< / option>
< option value =tKL> KL Sentral,KL< / option>
< option value =tipoh> Ipoh,Perak< / option>
< option value =tkulim> Kulim,Kedah< / option>
< option value =tkangar> Kangar,Perlis< / option>
< / select>
< label>日期:< / label>
< input name =dateetype =date/>
<! - 按钮选择时间表 - >
< a href =schedule.html>< button align =rightclass =buttonname =go> go< / button>< / a>
< / form>
< / div>
< div class =tr>
< img id ='train-route'
src =train_route.jpg
alt =
width =900
height =450 />
< / div>
< / body>
< / html>
我尝试过:
<?php
$ from = filter_input(INPUT_POST,'from');
$ to = filter_input(INPUT_POST,'to');
$ datee = filter_input(INPUT_POST,'datee');
if(!empty($ from)){
if(!empty($ to)){
if(!empty($ datee)){
$ host =localhost;
$ dbusername =root;
$ dbpassword =;
$ dbname =station;
//创建连接
$ conn = new mysqli($ host,$ dbusername,$ dbpassword,$ dbname);
if(mysqli_connect_error())
{
die('Connect Error('。mysqli_connect_errno()。')'。mysqli_connect_error());
}
else
{
$ sql =INSERT INTO dbstation(From,To,Date)值('$ from','$ to',' $日期E');
if($ conn-> query($ sql))
{
echo成功插入新记录;
}
else
{
echo错误:。$ sql。
。$ conn->错误;
}
$ conn-> close();
}
}
其他
{
// echo日期不应为空;
// die();
}
}
else {
// echo密码不应为空;
// die();
}
}
else {
// echo用户名不应为空;
// die();
}
?>
解决方案
I got an error says:
Error: INSERT INTO dbstation(From, To, Date) values('fAG', 'tkulim', '2018-09-30') You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'From, To, Date) values('fAG', 'tkulim', '2018-09-30')' at line 1
Can you help me to solve this problem? thanks in advance.
This is my html code:
<html> <head> <title>Main Menu</title> <link rel="stylesheet" type="text/css" href="menu_style.css"> <link rel="stylesheet" type="text/css" href="Datepicker.css"> </head> <body> <div class="menu"> <!-- <div class="btn"> --> <a href="main.html"><button class="btn">Booking</button></a> <a href="history.html"><button class="btn">History</button></a> <a href="reservation.html"><button class="btn">Reservation</button></a> <a href="trainpay.html"><button class="btn">TrainPay</button></a> <a href="reservation.html"><button class="btn">Update profile</button></a> <br><br><br> <!-- </div> --> <!-- <div class="station"> --> <form align="center" method="post" action="station.php"> From <select name="from"> <option value="fkluang">Kluang, Johor</option> <option value="fAG">Alor Gajah, Melaka</option> <option value="fSenawang">Senawang, NS</option> <option value="fKL">KL Sentral, KL</option> <option value="fipoh">Ipoh, Perak</option> <option value="fkulim">Kulim, Kedah</option> <option value="fkangar">Kangar, Perlis</option> </select> To <select name="to"> <option value="tkluang">Kluang, Johor</option> <option value="tAG">Alor Gajah, Melaka</option> <option value="tSenawang">Senawang, NS</option> <option value="tKL">KL Sentral, KL</option> <option value="tipoh">Ipoh, Perak</option> <option value="tkulim">Kulim, Kedah</option> <option value="tkangar">Kangar, Perlis</option> </select> <label>Date: </label> <input name="datee" type="date"/> <!-- Button to choose schedule --> <a href="schedule.html"><button align="right" class="button" name="go">go</button></a> </form> </div> <div class="tr"> <img id='train-route' src="train_route.jpg" alt="" width="900" height="450" /> </div> </body> </html>
What I have tried:
<?php $from = filter_input(INPUT_POST, 'from'); $to = filter_input(INPUT_POST, 'to'); $datee = filter_input(INPUT_POST, 'datee'); if (!empty($from)){ if (!empty($to)){ if (!empty($datee)){ $host = "localhost"; $dbusername = "root"; $dbpassword = ""; $dbname = "station"; // Create connection $conn = new mysqli ($host, $dbusername, $dbpassword, $dbname); if(mysqli_connect_error()) { die('Connect Error('.mysqli_connect_errno().')'.mysqli_connect_error()); } else { $sql = "INSERT INTO dbstation(From, To, Date) values('$from', '$to', '$datee')"; if($conn->query($sql)) { echo "New record is inserted successfully"; } else { echo "Error: ".$sql." ".$conn->error; } $conn->close(); } } else { // echo "Date should not be empty"; // die(); } } else{ // echo "Password should not be empty"; // die(); } } else{ // echo "Username should not be empty"; // die(); } ?>
解决方案
这篇关于如何使用PHP将select选项保存到数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!