Jump to content

I need to make multiple updates to a table at once


attaboy

Recommended Posts

I have this mysql table called playlist(see attachment) I have a php file called playlist_form.php which i use to generate a form with the contents of the playlist database it looks like this:

 

 

<?php
$con = new mysqli("localhost", "root", "root", "dpsite");
 
$sql = "SELECT * FROM playlist";
$res = mysqli_query($con,$sql);
if($res != FALSE) {
$toWrite = "";
while($row = mysqli_fetch_assoc($res)){
$toWrite .= "<form action='process_playlist.php' method='post'>\n";
$toWrite .= "<table>\n";
foreach($row as $key => $value){
$value = "\"".$value."\"";
if ($key == "song_title") $toWrite .= "<tr><td>Song Title: </td><td><input type='text' maxlength='30' name=".$key." value=".$value." /></td></tr><br>\n";
if ($key == "artist_name") $toWrite .= "<tr><td>Artist Name: </td><td><input type='text' maxlength='30' name=".$key." value=".$value." /></td></tr><br>\n";
if ($key == "image_path") $toWrite .= "<tr><td>Image: </td><td><input type='text' maxlength='30' name=".$key." value=".$value." /></td></tr><br>\n";
if ($key == "mp3_path") $toWrite .= "<tr><td>mp3: </td><td><input type='text' maxlength='30' name=".$key." value=".$value." /></td></tr><br>\n";
 
}
$toWrite .= "</table>";
}
 
$toWrite .="<p><input type='submit' name='submit' value='Submit' /></p>";
$toWrite .= "</form>";
echo $toWrite;
}else{
echo "form was not written!";
}
mysqli_close($con);
exit;
?>
 
The attachment generated_form shows you the output playlist_form.php generates.
To this point everything seems to work well.  I can enter new values into any of the songs I want.
When I submit this form I want truncate the table then enter the new data but I'm having trouble wrapping my head around this problem.  
 
This is what I've done so far:
 
<?php
$con = new mysqli("localhost", "root", "root", "dpsite");
 
if (isset($_POST["song_title"])) $title = $_POST["song_title"];
if (isset($_POST["artist_name"])) $artist = $_POST["artist_name"];
if (isset($_POST["image_path"])) $img = $_POST["image_path"];
if (isset($_POST["mp3_path"])) $mp3 = $_POST["mp3_path"];
//$url = $_POST["url_target"];
 
 
$sql = "INSERT INTO playlist(`song_title`,`artist_name`,`image_path`,`mp3_path`) VALUES('$title','$artist','$img','$mp3')";
 
$res = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($res)){
foreach($row as $key => $value){
 
if($title != "") {
 
if($res === TRUE) {
echo "data inserted";
}else{
echo "data was not inserted";
}
}
}
}
mysqli_close($con);
exit;
?> 
 
It's very incomplete but If someone can provide a simple example of how to update a table this way I'd be very grateful.  thanks

post-94869-0-67552000-1363927423_thumb.png

post-94869-0-58661200-1363927457_thumb.png

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.