看看我的代码,当我安装我的表时,我不知道该怎么办。而且,当将meno
和priezvisko
列与数据库中已有的值一起插入表单时,Im还试图防止它们的重复输入。
/ vytvorenie tabulky TRIEDA
$sql = "CREATE TABLE IF NOT EXISTS trieda(
id_triedy INT(4) NOT NULL AUTO_INCREMENT,
nazov CHAR(5),
PRIMARY KEY (id_triedy),
UNIQUE KEY (nazov))";
// vytvorenie tabulky STUDENT
$sql = "CREATE TABLE IF NOT EXISTS student(
id_student INT (5) NOT NULL AUTO_INCREMENT,
id_triedy INT (5) NOT NULL,
meno CHAR (15),
priezvisko CHAR (20),
PRIMARY KEY (`meno`, `priezvisko`),
KEY (id_student))";
// spustenie dopytu
if (mysqli_query($prip,$sql))
{
echo "Tabuľka vytvorená";
echo "<br />";
echo "<a href='./index.php'><strong>Späť</strong></a>";
}
else
{
echo "Chyba vytvorenia tabuľky: " . mysqli_error($prip);
echo "<br />";
echo "<a href='./index.php'><strong>Späť</strong></a>";
}
编辑:(给出错误:Chyba vytvorenia tabuľky:您的SQL语法有错误;请检查对应于MySQL服务器版本的手册,在“CREATE TABLE IF NOT EXISTS student(id_student INT(5)NOT)”附近的第7行使用正确的语法
// vytvorenie tabuliek do DB
$sql= "CREATE TABLE IF NOT EXISTS trieda(
id_triedy INT(4) NOT NULL AUTO_INCREMENT,
nazov CHAR(5),
PRIMARY KEY (id_triedy),
UNIQUE KEY (nazov));
CREATE TABLE IF NOT EXISTS student(
id_student INT (5) NOT NULL AUTO_INCREMENT,
id_triedy INT (5) NOT NULL,
meno CHAR (15),
priezvisko CHAR (20),
PRIMARY KEY (`meno`, `priezvisko`),
KEY (id_student))";
// spustenie dopytu
if (mysqli_query($prip,$sql))
{
echo "Tabuľka vytvorená";
....
编辑2:
if(isset($_POST['submit']))
{
//meno a priezvisko
$student = $_POST['meno'];
$priezvisko = $_POST['priezvisko'];
$id_trieda = $_GET['id_triedy'];
//connect to the database
include 'config.php';
//insert results from the form input
$row = mysqli_query("SELECT * FROM student WHERE meno='{$student}' AND priezvisko='{$priezvisko}'");
if($row){
echo 'Duplicate!';
}else {
mysqli_query("INSERT INTO student (meno, priezvisko, id_triedy) VALUES( '{$student}', '{$priezvisko}', {$id_trieda} )");
}
/* $add = "<table align='center'>
<tr>
<td> Študent bol úspešne pridaný do triedy. </td>
</tr>
<tr>
<td><a href='./trieda.php?id_triedy=".$_GET['id_triedy']."'><strong>Späť</strong></a></td>
</tr>
</table>";
$not_add = "<table align='center'>
<tr>
<td> Študent s týmto menom a priezviskom už je v tejto triede. </td>
</tr>
<tr>
<td><a href='./trieda.php?id_triedy=".$_GET['id_triedy']."'><strong>Späť</strong></a></td>
</tr>
</table>";
*/
mysqli_close($prip);
}
?>
最佳答案
问题可能是您正在覆盖$sql
。
$sql= "CREATE TABLE IF NOT EXISTS trieda(
id_triedy INT(4) NOT NULL AUTO_INCREMENT,
nazov CHAR(5),
PRIMARY KEY (id_triedy),
UNIQUE KEY (nazov));
CREATE TABLE IF NOT EXISTS student(
id_student INT (5) NOT NULL AUTO_INCREMENT,
id_triedy INT (5) NOT NULL,
meno CHAR (15),
priezvisko CHAR (20),
PRIMARY KEY (`meno`, `priezvisko`),
KEY (id_student))"
链接到mysqlfiddle。
编辑:
正如您可以看到的here,您的问题是
mysql_query()
上的PHP
不接受同一字符串中的许多查询,因此必须单独发送。$sql1 = "CREATE TABLE IF NOT EXISTS trieda(
id_triedy INT(4) NOT NULL AUTO_INCREMENT,
nazov CHAR(5),
PRIMARY KEY (id_triedy),
UNIQUE KEY (nazov));"
$sql2 = "CREATE TABLE IF NOT EXISTS student(
id_student INT (5) NOT NULL AUTO_INCREMENT,
id_triedy INT (5) NOT NULL,
meno CHAR (15),
priezvisko CHAR (20),
PRIMARY KEY (`meno`, `priezvisko`),
KEY (id_student));"
关于php - 安装表格并防止两列重复输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21262415/