Ich habe Probleme beim Einfügen von Daten in eine Tabelle. Ich habe die folgenden Tabellen: Titel (ID, Titel), Album (ID, Albumtitel), Komponist (ID, Komponist), Albumtrack (PRIMARY: Trackid, Albumid, Composerid).Einfügen von Daten in SQL-Tabellen
Auf meiner PHP-Seite können Sie einen Titel hinzufügen und dann das damit verbundene Album und den Komponisten auswählen. Es fügt den Track dem Track-Tab in Ordnung hinzu, fügt ihn aber keinem Album hinzu.
Ich suche weiter nach, wie es geht, und ich bin immer ein bisschen verloren. Kann mir jemand sagen, wie ich das richtig machen soll? Dank
if (isset($_POST['tracktitle'])):
// A new track has been entered
// using the form.
$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.</p>'); }
$sql = "INSERT INTO track, albumtrack SET
track.tracktitle='$tracktitle', albumtrack.albumid='$albs', albumtrack.composerid='$cid' " ;
if (@mysql_query($sql)) {
echo '<p>New track added</p>';
} else {
exit('<p>Error adding new track' . mysql_error() . '</p>');
}
$trackid = mysql_insert_id();
if (isset($_POST['albs'])) {
$albs = $_POST['albs'];
} else {
$albs = array();
}
$numAlbs = 0;
foreach ($albs as $albID) {
$sql = "INSERT IGNORE INTO albumtrack
SET albumtrack.trackid='$trackid', albumtrack.albumid='$albs', albumtrack.composerid='$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>
<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add another track</a></p>
<p><a href="tracks.php">Return to track search</a></p>
<?php
else: // Allow the user to enter a new track
$composers = @mysql_query('SELECT id, composername FROM composer');
if (!$composers) {
exit('<p>Unable to obtain composer list from the database.</p>');
}
$albs = @mysql_query('SELECT id, albumtitle FROM album');
if (!$albs) {
exit('<p>Unable to obtain album list from the database.</p>');
}
?>
<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['id'];
$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['id'];
$aname = htmlspecialchars($alb['albumtitle']);
echo "<label><input type='checkbox' name='albs[]' value='$aid' />$aname</label><br />\n";
}
?>
Lesen Sie ein wenig über SQL-Injection, Ihr aktueller Code ist anfällig ... – ChristopheD
Verwenden Sie MySQL? Wo hast du eine 'INSERT INTO tbl_a, tbl_b' Syntax gesehen? Das wird nicht funktionieren. – Konerak
Danke euch beiden. Ich kann mich nicht erinnern, woher ich diese Syntax habe. Ich habe es wahrscheinlich geträumt oder so. ! Prost – paj