Jump to content

I need to make multiple updates to a table at once


Go to solution Solved by 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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.