[英]What is wrong with my SQL Insert code?
I'm struggling with trying to find out why this code isn't working for me. 我正在努力找出为什么这段代码对我不起作用。 I have tables:
albums (albumid, albumname)
, composers (composerid, composername)
and tracks (trackid, tracktitle, albumid, composerid)
. 我有表格:
albums (albumid, albumname)
, composers (composerid, composername)
和tracks (trackid, tracktitle, albumid, composerid)
。
When I use my form to add a track and link it to a composer and an album from this: 当我使用表单添加曲目并将其链接到作曲家和专辑时:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Enter the new track:<br />
<textarea name="tracktitle" rows="1" cols="20"></textarea></p>
<p>Composer: <select name="cid" size="1">
<option selected value="">Select One</option>
<option value="">---------</option>
<?php while ($composer= mysql_fetch_array($composers)) {
$cid = $composer['composerid'];
$cname = htmlspecialchars($composer['composername']);
echo "<option value='$cid'>$cname</option>\n";} ?>
</select></p>
<p>Place in albums:<br />
<?php while ($alb = mysql_fetch_array($albs)) {
$aid = $alb['albumid'];
$aname = htmlspecialchars($alb['albumname']);
echo "<label><input type='checkbox' name='albs[]'
value='$aid' />$aname</label><br />\n";
} ?>
</p>
<input type="submit" value="SUBMIT" />
</form>
<?php endif; ?>
I get this message: 我收到此消息:
New track added
添加了新曲目
Error inserting track into album 2:将曲目插入专辑2时出错:
Track was added to 0 albums.曲目已添加到0个相册。
The php code that precedes the form is: 表单之前的php代码为:
if (isset($_POST['tracktitle'])):
// A new track has been entered
// using the form.
$tracktitle = mysql_real_escape_string($tracktitle);
$cid= $_POST['cid'];
$tracktitle = $_POST['tracktitle'];
$albs = $_POST['albs'];
if ($cid == '') {
exit('<p>You must choose an composer for this track. Click
"Back" and try again. “返回”,然后重试。
$sql = "INSERT INTO tracks (tracktitle) VALUES ('$tracktitle')" ; if (@mysql_query($sql)) { echo '<p>New track added</p>'; } else { exit('<p>Error adding new track' . mysql_error() . '</p> echo mysql_error() ');} $trackid = mysql_insert_id(); if (isset($_POST['albs'])) { $albs = $_POST['albs']; } else { $albs = array(); } $numAlbs = 0; foreach ($albs as $albID) { $sql = "INSERT IGNORE INTO tracks (trackid, albumid, composerid) VALUES " . "($trackid, $albs, $cid)"; if ($ok) { $numAlbs = $numAlbs + 1; } else { echo "<p>Error inserting track into album $albID: " . mysql_error() . '</p>'; }}?> <p>Track was added to <?php echo $numAlbs; ?> albums.</p> <?php else: // Allow the user to enter a new track $composers = @mysql_query('SELECT composerid, composername FROM composers'); if (!$composers) { exit('<p>Unable to obtain composer list from the database.</p>'); } $albs = @mysql_query('SELECT albumid, albumname FROM albums'); if (!$albs) { exit('<p>Unable to obtain album list from the database.</p>');}?>
I keep searching for why this is failing and I keep hitting brick walls. 我一直在寻找失败的原因,而且还在不断撞墙。 I also know that at present it's not very secure which will be the next thing I sort out.
我还知道,目前还不是很安全,这是我接下来要解决的问题。 I just want to get the actual function working first.
我只想让实际功能先工作。
@paj: Change @paj:更改
if ($ok) {
to 至
if (mysql_query($sql)) {
- --
I also suggest you update your SQL statements to 我还建议您将SQL语句更新为
$sql = "INSERT INTO tracks (tracktitle) VALUES ('" . $tracktitle . "')";
$sql = "INSERT IGNORE INTO tracks (trackid, albumid, composerid) VALUES (" . $trackid . ", " . $albID . ", " . $cid . ")";
Looks to me like $ok
doesn't exist except in the if ($ok) {
line. 在我看来,
$ok
if ($ok) {
不存在,除非在if ($ok) {
行中。 It needs to be defined somewhere prior, otherwise it will always read false because it doesn't exist. 需要在某个地方定义它,否则它将始终显示为false,因为它不存在。
Actually you can skip the $ok which doesn't exist and put in if (@mysql_query($sql)) {
for that line like you have above. 实际上,您可以跳过不存在的$ ok,并在
if (@mysql_query($sql)) {
插入上面的代码。 I do have to agree with the comments that the code needs some love, but if you want to know why it's breaking down, it appears this is why. 我确实必须同意注释,即代码需要一些爱,但是如果您想知道为什么它崩溃了,看来这就是原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.