Jump to content

[SOLVED] Update record using checkboxes


rhiaro

Recommended Posts

I currently have a form to update existing records in a database, which all works fine now.

 

The database contains information about shows on a radio station.  Some shows are on one day, some are on a few different days, ie. one might be on Mondays, one might be Mondays, Wednesdays and Fridays, etc.

I want to include as part of the form checkboxes for each day of the week, so the information about what day each show is on can be updated by checking or unchecking the appropriate boxes.

 

I have so far created seven new fields in my database, one for each day of the week, the idea being that they are set to either true or false, depending on the days the show is on.  Is this the most sensible way of going about it?

 

Now I'm just struggling with making the checkboxes work.  I need to display them as part of the form either checked or unchecked - depending on what is already in the database (ie if they have already been set to true or false). 

Then the checking or unchecking of the boxes by the user must update the database.

 

I hope that's clear...  I have scoured the internet for a solution, but can't seem to find anything that helps  :(

 

The code for the pages involved is as follows:

 

To choose which show to update:

<html>
<head>
<title>Update Shows</title>
</head>

<body bgcolor="white">

<form method="POST" action="updateform.php">

Show to update: <input type="text" name="id"><br>
<input type="submit" value="Submit">

</form>

<br><br>
<b>Show IDs</b><br>

<?php
mysql_connect("localhost", "rhiaroco_siren", "password") or die("Connection Failed");

mysql_select_db("rhiaroco_siren")or die("Connection Failed");


$result = mysql_query("SELECT * FROM showinfo");

while($row = mysql_fetch_array($result))
  {
  echo $row['id'] . " - " . $row['ShowName'];
  echo "<br />";
  }


?>

</body>
</html>

 

The form page:

<?php
foreach($HTTP_POST_VARS as $varname => $value)
$formVars[$varname]=$value;


mysql_connect("localhost", "rhiaroco_siren", "password") or die("Connection Failed");

mysql_select_db("rhiaroco_siren")or die("Connection Failed");


$query="SELECT * FROM showinfo WHERE id = \"".$formVars["id"]."\"";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$formVars = array();
$formVars["ShowName"]=$row["ShowName"];
$formVars["DJ"]=$row["DJ"];
$formVars["Day"]=$row["Day"];
$formVars["StartTime"]=$row["StartTime"];
$formVars["EndTime"]=$row["EndTime"];
$formVars["ShowDesc"]=$row["ShowDesc"];
$formVars["DJInfo"]=$row["DJInfo"];

$formVars["Monday"]=$row["Monday"];
$formVars["Tuesday"]=$row["Tuesday"];
$formVars["Wednesday"]=$row["Wednesday"];
$formVars["Thursday"]=$row["Thursday"];
$formVars["Friday"]=$row["Friday"];
$formVars["Saturday"]=$row["Saturday"];
$formVars["Sunday"]=$row["Sunday"];

$formVars["id"]=$row["id"];


?>

<html>
<form method="post" name="update" action="update.php" />

Show Name: <input type="text" name="ShowName" value="<? echo ($formVars["ShowName"]); ?>" />
<br />
Presenter: <input type="text" name="DJ" value="<? echo ($formVars["DJ"]); ?>" />
<br />
Days: <br />
Monday: <input type="checkbox" name="Monday" value="Y" >
<br />
From: <input type="text" name="StartTime" value="<? echo ($formVars["StartTime"]); ?>" />
<br />
Until: <input type="text" name="EndTime" value="<? echo ($formVars["EndTime"]); ?>" />
<br />
Show Description: <input type="text" name="ShowDesc" value="<? echo ($formVars["ShowDesc"]); ?>" />
<br />
DJ Info: <input type="text" name="DJInfo" value="<? echo ($formVars["DJInfo"]); ?>" />

<input type="hidden" name="id" value="<? echo($formVars["id"]); ?>">

<input type="submit" name="Submit" value="update" />
</form>
</html>

 

The update page:

<?php
mysql_connect("localhost", "rhiaroco_siren", "password") or die("Connection Failed");

mysql_select_db("rhiaroco_siren")or die("Connection Failed");

$id = $_POST['id'];
$ShowName = $_POST['ShowName'];
$DJ = $_POST['DJ'];
$StartTime = $_POST['StartTime'];
$EndTime = $_POST['EndTime'];
$ShowDesc = $_POST['ShowDesc'];
$DJInfo = $_POST['DJInfo'];
$Monday = $_POST['Monday'];
$Tuesday = $_POST['Tuesday'];
$Wednesday = $_POST['Wednesday'];
$Thursday = $_POST['Thursday'];
$Friday = $_POST['Friday'];
$Saturday = $_POST['Saturday'];
$Sunday = $_POST['Sunday'];


$query = "UPDATE showinfo SET ".
"ShowName = '$ShowName'".",".
"DJ = '$DJ'".",".
"StartTime = '$StartTime'".",".
"EndTime = '$EndTime'".",".
"ShowDesc = '$ShowDesc'".",".
"DJInfo = '$DJInfo'".

"WHERE id = '$id'";

if(mysql_query($query)){
echo "Information Updated.<br>Click <a href=\"choose_show_to_update.php\">here</a> to update another show.";}
else{
echo "Could not be updated";}
?>

 

Thanks in advance for help :)

Link to comment
https://forums.phpfreaks.com/topic/144812-solved-update-record-using-checkboxes/
Share on other sites

Well, solved it now anyway...

 

In the form:

Monday: <input type="checkbox" name="Monday" value="Y" <?php if($formVars['Monday']=='Y') echo 'checked'; ?>><br />
Tuesday: <input type="checkbox" name="Tuesday" value="Y" <?php if($formVars['Tuesday']=='Y') echo 'checked'; ?>><br />
Wednesday: <input type="checkbox" name="Wednesday" value="Y" <?php if($formVars['Wednesday']=='Y') echo 'checked'; ?>><br />
Thursday: <input type="checkbox" name="Thursday" value="Y" <?php if($formVars['Thursday']=='Y') echo 'checked'; ?>><br />
Friday: <input type="checkbox" name="Friday" value="Y" <?php if($formVars['Friday']=='Y') echo 'checked'; ?>><br />
Saturday: <input type="checkbox" name="Saturday" value="Y" <?php if($formVars['Saturday']=='Y') echo 'checked'; ?>><br />
Sunday: <input type="checkbox" name="Sunday" value="Y" <?php if($formVars['Sunday']=='Y') echo 'checked'; ?>><br />

 

And the query:

$query="UPDATE showinfo SET 
ShowName = '".$ShowName."', 
DJ = '".$DJ."', 
StartTime = '".$StartTime."', 
EndTime = '".$EndTime."', 
ShowDesc = '".$ShowDesc."', 
DJInfo = '".$DJInfo."', 
Monday = '".(($Monday=='Y')?'Y':'N')."',
Tuesday = '".(($Tuesday=='Y')?'Y':'N')."',
Wednesday = '".(($Wednesday=='Y')?'Y':'N')."',
Thursday = '".(($Thursday=='Y')?'Y':'N')."',
Friday = '".(($Friday=='Y')?'Y':'N')."',
Saturday = '".(($Saturday=='Y')?'Y':'N')."',
Sunday = '".(($Sunday=='Y')?'Y':'N')."'

WHERE id = '".$id."' limit 1";

 

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.