Jump to content

[SOLVED] PHP form for Editing a MySQL database


csteff24

Recommended Posts

I'm just learning PHP and I'm using it to make a website for my school - (I'm hosting it at http://www.csteffen.com/staples  while it's under construction)

I have a database of clubs, and I would like a form that the club advisors can access in order to update the information

 

I've tried to set it up on my own, but I'm getting a few errors and I have no idea if this is how it should be set up.

 

<?php
include("include.php");

if (!$_POST) {


$get_list_sql = "SELECT id, name FROM clubs ORDER BY name";
$get_list_res = mysqli_query($mysqli, $get_list_sql)
     or die(mysqli_error($mysqli));

if (mysqli_num_rows($get_list_res) < 1) {
$display_block .= "<p><em>Sorry, no clubs to select!</em></p>";
}
else{
$display_block .= "
<form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">
<p><strong>Select a Club to Edit:</strong><br/>
<select name=\"sel_id\">
<option value=\"\">-- Select One --</option>";

while($recs = mysqli_fetch_array($get_list_res)) {
     $id = $recs['id'];
     $name = $recs['name'];
     $display_block .= "<option value=\"".$id."\">".
     $name ."</option>";
}

     $display_block ="

     <p><strong>Faculty Advisor:</strong><br/>
     <input type=\"text\" name=\"adv\" size=\"30\" maxlength=\"50\"></p>

     <p><strong>Meeting time/place:</strong><br/>
     <input type=\"text\" name=\"meet\" size=\"30\" maxlength=\"200\">

     <p><strong>Student President - (if multiple, use &):</strong><br/>
     <input type=\"text\" name=\"pres\" size=\"30\" maxlength=\"50\">

     <p><strong>President contact info:</strong><br/>
     <input type=\"text\" name=\"email\" size=\"30\" maxlength=\"50\">

     <p><strong>Club Description:</strong><br/>
     <textarea name=\"descrip\" cols=\"50\" rows=\"5\"
     wrap=\"virtual\"></textarea></p>

     <p><input type=\"submit\" name=\"submit\" value=\"Edit Club\"></p>
     </form>";

} else if ($_POST) {
     if (($_POST["name"] == "")) {
           header("Location: add.php");
           exit;
}

doDB();

    $query = "UPDATE clubs SET adv = '".$_POST["adv"]."' AND  meet = '".$_POST["meet"]."' AND  pres = '".$_POST["pres"]."' AND  email = '".$_POST["email"]."' AND  descrip = '".$_POST["descrip"]."'WHERE id = ".$id.""; //  

    $result = mysql_query($query) or die( "An error has occurred: " .mysql_error (). ":" .mysql_errno ());

mysqli_close($mysqli);
$display_block = "<p>Your entry has been edited.";
}
?>

<html>
<head>
<title>Edit Club</title>
</head>
<body>
<h1>Edit a Club</h1>
<?php echo $display_block; ?>
</body>
</html>

 

 

Any ideas? Thank you!

Here's my new, semi-changed code: no errors are coming up, but the form doesn't submit ...

 

<?php
include("include.php");
doDB();

if (!$_POST) {

$get_list_sql = "SELECT id, name FROM clubs ORDER BY name";
$get_list_res = mysqli_query($mysqli, $get_list_sql)
     or die(mysqli_error($mysqli));

$display_block .= "
<form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">
<p><strong>Select a Club to Edit:</strong><br/>
<select name=\"sel_id\">
<option value=\"\">-- Select Club --</option>";

while($recs = mysqli_fetch_array($get_list_res)) {
     $id = $recs['id'];
     $name = $recs['name'];
     $display_block .= "<option value=\"".$id."\">".
     $name ."</option>";
}

     $display_block .= "
</select>

     <p><strong>Faculty Advisor:</strong><br/>
     <input type=\"text\" name=\"adv\" size=\"30\" maxlength=\"50\"></p>

     <p><strong>Meeting time/place:</strong><br/>
     <input type=\"text\" name=\"meet\" size=\"30\" maxlength=\"200\">

     <p><strong>Student President - (if multiple, use &):</strong><br/>
     <input type=\"text\" name=\"pres\" size=\"30\" maxlength=\"50\">

     <p><strong>President contact info:</strong><br/>
     <input type=\"text\" name=\"email\" size=\"30\" maxlength=\"50\">

     <p><strong>Club Description:</strong><br/>
     <textarea name=\"descrip\" cols=\"50\" rows=\"5\"
     wrap=\"virtual\"></textarea></p>

     <p><input type=\"submit\" name=\"submit\" value=\"Edit Club\"></p>
     </form>";

mysqli_free_result($get_list_res);

} else if ($_POST) {
     if ($_POST["name"] == "") {
           header("Location: clubedit.php");
           exit;
}

    $update_clubs_sql = "UPDATE clubs SET adv = '".$_POST["adv"]."' AND  meet = '".$_POST["meet"]."' AND  pres = '".$_POST["pres"]."' AND  email = '".$_POST["email"]."' AND  descrip = '".$_POST["descrip"]."' WHERE id = '".$_POST["sel_id"]."'"; //  

    $update_clubs_res = mysqli_query($mysqli, $update_clubs_sql)
         or die (mysqli_error($mysqli));

$display_block = "<p>Your entry has been edited.";
}
?>

<html>
<head>
<title>Edit Club</title>
</head>
<body>
<h1>Edit a Club</h1>
<?php echo $display_block; ?>
</body>
</html>

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.