我的 SQL 插入代码有什么问题?

发布于 2024-10-05 10:27:32 字数 2932 浏览 4 评论 0原文

我正在努力找出为什么这段代码对我不起作用。我有表:专辑(albumid,albumid)作曲家(composerid,composername)曲目(trackid,tracktitle,albumid,composerid)

当我使用表单添加曲目并将其链接到作曲家和专辑时:

<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; ?>

我收到以下消息:

添加了新曲目
将曲目插入专辑 2 时出错:
曲目已添加到 0 个专辑。

表单前面的 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 

“返回”并重试。

');}

$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'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).

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:
Track was added to 0 albums.

The php code that precedes the form is:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪意 2024-10-12 10:27:32

@paj:更改

if ($ok) {

if (mysql_query($sql)) {

-

我还建议您将 SQL 语句更新为

$sql = "INSERT INTO tracks (tracktitle) VALUES ('" . $tracktitle . "')";

$sql = "INSERT IGNORE INTO tracks (trackid, albumid, composerid) VALUES (" . $trackid . ", " . $albID . ", " . $cid . ")";

@paj: Change

if ($ok) {

to

if (mysql_query($sql)) {

-

I also suggest you update your SQL statements to

$sql = "INSERT INTO tracks (tracktitle) VALUES ('" . $tracktitle . "')";

$sql = "INSERT IGNORE INTO tracks (trackid, albumid, composerid) VALUES (" . $trackid . ", " . $albID . ", " . $cid . ")";
天邊彩虹 2024-10-12 10:27:32

在我看来,除了 if ($ok) { 中之外, $ok 不存在
线。它需要事先在某个地方定义,否则它总是会读为 false,因为它不存在。

实际上,您可以跳过不存在的 $ok 并像上面那样为该行添加 if (@mysql_query($sql)) { 。我确实必须同意代码需要一些爱的评论,但如果你想知道它为什么会崩溃,看来这就是原因。

Looks to me like $ok doesn't exist except in the if ($ok) {
line. It needs to be defined somewhere prior, otherwise it will always read false because it doesn't exist.

Actually you can skip the $ok which doesn't exist and put in if (@mysql_query($sql)) { for that line like you have above. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文