我正在努力解决这两个输入INSERT查询问题。它不会向db插入任何内容。首先,它应该接受用户输入和下拉菜单选择,这是来自另一个表的id因此SELECT查询。修复此代码后,我将通过'pubid'设置两个表之间的关系。
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
require_once("dbconn.php");
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf8",$dbuser,$dbpass,$dbo);
} catch (PDOException $e) {
echo $e->getMessage();
exit;
}
if(isset($_POST['addBtn'])){
$q = $conn->prepare("INSERT INTO series (title, pubid) VALUES (:title, :pubid)");
$q->bindParam(':title',$title,PDO::PARAM_STR);
$q->bindParam(':pubid',$pubid,PDO::PARAM_INT);
$q->execute();
}
$sql = 'SELECT pubid, name FROM publisher ORDER BY name';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="eng">
<head>
<meta charset="UTF-8">
<title>
Comics DB > Add Series
</title>
<link rel="stylesheet" type="text/css" href="comicsdb.css">
</head>
<body>
<div class="main">
<menu>
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="add.php">Add</a></li>
<li><a href="edit.php">Edit</a></li>
<li><a href="delete.php">Delete</a></li>
<li><a href="list.php">List</a></li>
<li><a href="search.php">Search</a></li>
</ul>
</menu>
<div class="pub_menu">
<form action="addseries.php" method="POST">
<p>
Series:
<input type="text" name="title" id="title" size="40" /><br />
<select>
<option>Select</option>
<?php while ($row = $q->fetch()){ ?>
<option name="pubid" id="pubid" value="<?php echo $row['pubid']; ?>"><?php echo $row['name']; ?></option>
<?php } ?>
</select>
<input type="submit" value="Add Series" name="addBtn" />
</p>
</form>
</div>
</body>
</html>
dbconn.php
<?php
$dbhost = '127.0.0.1';
$dbname = 'comicsdb';
$dbuser = 'root';
$dbpass = 'FuckYou';
$dbport = '3306';
$charset = 'utf8';
$dbo = array(
// important! use actual prepared statements (default: emulate prepared statements)
PDO::ATTR_EMULATE_PREPARES => false
// throw exceptions in case of errors (default: stay silent)
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
// fetch associative arrays (default: mixed arrays)
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
[编辑]
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
require_once("dbconn.php");
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf8",$dbuser,$dbpass,$dbo);
} catch (PDOException $e) {
echo $e->getMessage();
exit;
}
if(isset($_POST['addBtn'])){
$title = $_POST['title'];
$pubid = $_POST['pubid'];
$q = $conn->prepare("INSERT INTO series (title, pubid) VALUES (:title, :pubid)");
$q->bindParam(':title',$title,PDO::PARAM_STR);
$q->bindParam(':pubid',$pubid,PDO::PARAM_INT);
$q->execute();
}
$sql = 'SELECT pubid, name FROM publisher ORDER BY name';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="eng">
<head>
<meta charset="UTF-8">
<title>
Comics DB > Add Series
</title>
<link rel="stylesheet" type="text/css" href="comicsdb.css">
</head>
<body>
<div class="main">
<menu>
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="add.php">Add</a></li>
<li><a href="edit.php">Edit</a></li>
<li><a href="delete.php">Delete</a></li>
<li><a href="list.php">List</a></li>
<li><a href="search.php">Search</a></li>
</ul>
</menu>
<div class="pub_menu">
<form action="addseries.php" method="POST">
<p>
Series:
<input type="text" name="title" id="title" size="40" /><br />
<select>
<option name="pubid" id="pubid">Select</option>
<?php while ($row = $q->fetch()){ ?>
<option value="<?php echo $row['pubid']; ?>"><?php echo $row['name']; ?></option>
<?php } ?>
</select>
<input type="submit" value="Add Series" name="addBtn" />
</p>
</form>
</div>
</body>
</html>
答案 0 :(得分:1)
您没有为变量分配任何内容。
将它们分配给POST阵列:
if(isset($_POST['title'],$_POST['pubid'])){
$title = $_POST['title'];
$pubid = $_POST['pubid'];
$q = $conn->prepare("INSERT INTO publisher (title, pubid) VALUES (:title, :pubid)");
$q->bindParam(':title',$title,PDO::PARAM_STR);
$q->bindParam(':pubid',$pubid,PDO::PARAM_INT);
$q->execute();
}
<select>
带有name属性,而不是<option>
。
将您的<select>
更改为<select name="pubid">
,然后从name="pubid"
中移除<option>
。
修改强>
根据您的编辑,您仍然拥有选择的未命名属性:
<select>
<option name="pubid" id="pubid">Select</option>
需要阅读:
<select name="pubid">
<option>Select</option>
这就是为什么你仍然得到一个未定义的索引通知。
另外,你可以在里面移动你的身份:
<select name="pubid" id="pubid">