Jump to content

what did i do wrong?


ansipants

Recommended Posts

Can someone please take a look and see if you can point out what error i made. Everything on the form side is functioning except it doesn't seem to post since its looping back to the form rather than the redirect. Also, within the actual form table, i had to change the section that says Business Hours from a textarea because I couldn't get it to echo the data from the field in the database. Any ideas on that would be greatly appreciated. Sorry if its a bit sloppy, I'm no expert.

 

<?php
   include "config.php";
mysql_connect("$dbhost","$dbusr","$dbpass"); 
mysql_select_db("$dbname");
            
session_start();

if (isset($_SESSION['user'])) {
   
      } else {
    header ('location: login.php');
}

// creates the edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($id, $username, $location, $address, $city, $zip, $county, $website, $map, $email, $phone, $bhours, $ptimes, $pspecials, $vspecials, $error)
{

echo "<center><strong>Edit Location Information</strong></center>";

// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}

echo "
<center>

    <form action='' method='POST'>
   
      <table border='0' frame='box'>
            <tr>
              <td width='96'><div align='right'>Venue ID:</div></td>
              <td colspan='2'><input type='hidden' name='id' value='$id'>$id</td>
            </tr>
            <tr>
                <td><div align='right'><font size='2' face='arial'>Username:</font></div></td>
                <td colspan='2'><input type='text' name='r_username' value='$username'></td>
            </tr>
         <tr>
           <td>Business Name:</td>
           <td colspan='2'><input type='text' name='r_location' value='$location'></td>
        </tr>
         <tr>
                <td><div align='right'><font size='2' face='arial'>Address:</font></div></td>
                <td colspan='2'><input type='text' name='r_address' value='$address'></td>
            </tr>
         <tr>
           <td><div align='right'>City/State:</div></td>
           <td><input type='text' name='r_city' value='$city'></td>
           <td>Format = City, St</td>
         </tr>
         <tr>
                <td><div align='right'><font size='2' face='arial'>Zip:</font></div></td>
                <td colspan='2'><input type='text' name='r_zip'value='$zip'></td>
            </tr>
            <tr>
              <td><div align='right'>County:</div></td>
              <td colspan='2'><input type='text' name='r_county'value='$county'></td>
            </tr>
            <tr>
              <td><div align='right'>Website:</div></td>
              <td colspan='2'><input type='text' name='r_website' value='$website'></td>
            </tr>
            <tr>
              <td><div align='right'>Directions:</div></td>
              <td colspan='2'><input type='text' name='r_map' value='$map'></td>
            </tr>
            <tr>
              <td><div align='right'><font size='2' face='arial'>E-mail:</font></div></td>
              <td colspan='2'><input type='text' name='r_email' value='$email'></td>
            </tr>
            <tr>
                <td><div align='right'><font size='2' face='arial'>Phone #::</font></div></td>
                <td colspan='2'><input type='text' name='r_phone' value='$phone'></td>
            </tr>
            <tr>
                <td height='80'><div align='right'><font size='2' face='arial'>Business Hours:</font></div></td>
                <td width='287'><input type='text' name='r_bhours' cols='45' rows='3' value='$bhours'></td>
                <td width='244'><p>**You must enter </br> after each line to display in rows:</p>
                <p>Mon - Fri 11am to 10pm</br></p>
                <p>Sat - Sun 1PM to 3am</br></p></td>
            </tr>
            <tr>
                <td><div align='right'><font size='2' face='arial'>Poker Times:</font></div></td>
                <td colspan='2'><input type='text' name='r_ptimes' value='$ptimes'></td>
            </tr>
            <tr>
              <td><div align='right'><font size='2' face='arial'>Poker Specials:</font></div></td>
              <td><input type='text' name='r_pspecials' value='$pspecials'></td>
              <td> </td>
            </tr>
            <tr>
              <td><div align='right'><font size='2' face='arial'>Location Specials:</font></div></td>
              <td><input type'text' name='r_vspecials' cols='45' rows='3' value='$vspecials'></td>
              <td><p>**You must enter </br> after each line to display in rows:</p>
                <p>Mon - $0.10 Wings</br></p>
              <p>Fri - 2 for 1 Happy hour</br></p></td>
            </tr>
            <tr>
                <td></td>
                <td colspan='2'><div align='center'><font size='2' face='arial'><a href='edituser.php'>Cancel</a> - </font><input type='submit' value='Update Location'></div></td>
            </tr>
        </table>
</form>
    
</center>
";
}

// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{ 
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['id']))
{
// get form data, making sure it is valid
$id = $_POST['id'];
$username = mysql_real_escape_string(htmlspecialchars($_POST['username']));
$location = mysql_real_escape_string(htmlspecialchars($_POST['loc_name']));
$address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
$city = mysql_real_escape_string(htmlspecialchars($_POST['city']));
$zip = mysql_real_escape_string(htmlspecialchars($_POST['zip']));
$county = mysql_real_escape_string(htmlspecialchars($_POST['county']));
$website = mysql_real_escape_string(htmlspecialchars($_POST['website']));
$map = mysql_real_escape_string(htmlspecialchars($_POST['map']));
$email = mysql_real_escape_string(htmlspecialchars($_POST['email']));
$phone = mysql_real_escape_string(htmlspecialchars($_POST['phone']));
$bhours = mysql_real_escape_string(htmlspecialchars($_POST['hours']));
$ptimes = mysql_real_escape_string(htmlspecialchars($_POST['ptime']));
$pspecials = mysql_real_escape_string(htmlspecialchars($_POST['pspecial']));
$vspecials = mysql_real_escape_string(htmlspecialchars($_POST['vspecial']));
// check that username field is filled in
if ($username == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';

//error, display form
renderForm($id, $username, $location, $address, $city, $zip, $county, $website, $map, $email, $phone, $bhours, $ptimes, $pspecials, $vspecials, $error);
}
else
{
// save the data to the database
mysql_query("UPDATE vusr SET username='$username', loc_name='$location', address='$address', city='$city', zip='$zip', county='$county', website='$website', map='$map', email='$email', phone='$phone', hours='$bhours', ptime='$ptimes', pspecial='$pspecials', vspecial='$vspecials' WHERE id='$id'")
or die(mysql_error()); 

// once saved, redirect back to the view page
header("Location: edituser.php"); 
}
}
else
{
// if the 'id' isn't valid, display an error
echo 'Error!';
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{

// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// query db
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM vusr WHERE id=$id")
or die(mysql_error()); 
$row = mysql_fetch_array($result);

// check that the 'id' matches up with a row in the databse
if($row)
{

// get data from db
$id = $row['id'];
$username = $row['username'];
$location = $row['loc_name'];
$address = $row['address'];
$city = $row['city'];
$zip = $row['zip'];
$county = $row['county'];
$website = $row['website'];
$map = $row['map'];
$email = $row['email'];
$phone = $row['phone'];
$bhours = $row['hours'];
$ptimes = $row['ptime'];
$pspecials = $row['pspecial'];
$vspecials = $row['vspecial'];
// show form
renderForm($id, $username, $location, $address, $city, $zip, $county, $website, $map, $email, $phone, $bhours, $ptimes, $pspecials, $vspecials, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error!';
}
}


?>

 

MOD EDIT: code tags added.

Link to comment
https://forums.phpfreaks.com/topic/248275-what-did-i-do-wrong/
Share on other sites

Your form does not have a field with a name='submit' attribute, so your form processing code, which tests if (isset($_POST['submit'])) will never execute.

 

You need to have a form field with name='submit'. I recommend a hidden field with that name so that all browsers will send the value regardless of how the form gets submitted.

Link to comment
https://forums.phpfreaks.com/topic/248275-what-did-i-do-wrong/#findComment-1274952
Share on other sites

Your form does not have a field with a name='submit' attribute, so your form processing code, which tests if (isset($_POST['submit'])) will never execute.

 

You need to have a form field with name='submit'. I recommend a hidden field with that name so that all browsers will send the value regardless of how the form gets submitted.

 

  <td colspan='2'><div align='center'><font size='2' face='arial'><a href='edituser.php'>Cancel</a> - </font><input type='submit' value='Update Location'></div></td>

Link to comment
https://forums.phpfreaks.com/topic/248275-what-did-i-do-wrong/#findComment-1274956
Share on other sites

ok so i added the name for the submit and how it spits out an error that says Notice: Undefined index that refers to the following lines of code:

 

$username = mysql_real_escape_string(htmlspecialchars($_POST['username']));
$location = mysql_real_escape_string(htmlspecialchars($_POST['loc_name']));
$address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
$city = mysql_real_escape_string(htmlspecialchars($_POST['city']));
$zip = mysql_real_escape_string(htmlspecialchars($_POST['zip']));
$county = mysql_real_escape_string(htmlspecialchars($_POST['county']));
$website = mysql_real_escape_string(htmlspecialchars($_POST['website']));
$map = mysql_real_escape_string(htmlspecialchars($_POST['map']));
$email = mysql_real_escape_string(htmlspecialchars($_POST['email']));
$phone = mysql_real_escape_string(htmlspecialchars($_POST['phone']));
$bhours = mysql_real_escape_string(htmlspecialchars($_POST['hours']));
$ptimes = mysql_real_escape_string(htmlspecialchars($_POST['ptime']));
$pspecials = mysql_real_escape_string(htmlspecialchars($_POST['pspecial']));
$vspecials = mysql_real_escape_string(htmlspecialchars($_POST['vspecial']));

Link to comment
https://forums.phpfreaks.com/topic/248275-what-did-i-do-wrong/#findComment-1274966
Share on other sites

:-[

 

yep that did it.

 

any ideas on how i can echo the php data into the textarea? the original line read like this:

 

<textarea name='r_bhours' cols='45' rows='3' value='$bhours'></textarea>

 

rather than:

 

<input type='text' name='r_bhours' cols='45' rows='3' value='$bhours'>

 

haven't cleaned up the code yet, as you can see i changed the part that says textarea to input so that i could test it and it worked the second way, but i need it the first.

Link to comment
https://forums.phpfreaks.com/topic/248275-what-did-i-do-wrong/#findComment-1274984
Share on other sites

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.