Jump to content

Editing A User's Record


Go to solution Solved by Ch0cu3r,

Recommended Posts

So I've created a form that allows an estate agent to store customer information. I am now trying to allow this information to be edited in a HTML form. I can't figure out how to get the selected row to be echoed into my form fields and then updated back into the database. Here is my code so far.

 

if (isset($_POST['showcatalogue']))
{
$showallcustomers = "SELECT * from CustomerTable";
$allcustomerresults = mysql_query($showallcustomers);
echo"<div class='page-restrict-output'>";
echo "<form method='post' action='customer-viewer'>";
echo "<input id='editcustomerbutton' type='Submit' name='editcustomerbtn' value='Edit Customer' /><br><br /></td></tr>";
echo "Here are the results, Select only ONE Customer to edit or the operation will FAIL. <br>";
echo "<br>";
echo"<table id='selectcustomertable' border='1'>"; 
echo "<tr><th>FirstName</th><th>SecondName</th><th>Address Line 1</th><th>Address Line 2</th><th>Address Line 3</th><th>Post Code</th><th>Customer ID</th><th>Phone Number</th><th>E-Mail</th><th>Edit Customer</th></tr>"; 
while($row = mysql_fetch_array($allcustomerresults)) { 
echo "<tr><td>"; 
echo $row['FirstName']; 
echo "</td><td>";
echo $row['SecondName'];  
echo "</td><td>";
echo $row['AddressLine1'];  
echo "</td><td>";
echo $row['AddressLine2'];  
echo "</td><td>";
echo $row['AddressLine3'];  
echo "</td><td>";
echo $row['PostCode'];  
echo "</td><td>";
echo $row['CustomerID'];  
echo "</td><td>";
echo $row['PhoneNumber'];  
echo "</td><td>";
echo $row['EMail'];  
echo "</td><td>";
echo $row['EditCustomer']; 
echo "<input id='selectcustomerbutton' type='checkbox' name='selectedCustomerIDS[]' value='$row[CustomerID]'  /></td></tr>";
echo" </form>";
echo "</div>";


}
$_POST['selectedCustomerIDS'];
{
echo "<form id=alleditcustomerform action='customer-viewer' autocomplete='on' method='post' onsubmit='return(validatecustomer());' >";
echo "<input type='hidden' name='savechanges' value='true' />";
echo "<br>";
foreach($_POST['selectedCustomerIDS'] as $CustomerIDforedit){
echo " You have selected Customer Number $CustomerIDforedit";
$theselectedrow = "SELECT row from CustomerTable WHERE '$CustomerIDforedit' = '$CustomerID'";
$theselectedrowresults = mysql_query($theselectedrow);
$count = mysql_num_rows($theselectedrowresults);


if ($count == 1) {


$FirstName = $_GET['FirstName'];
$SecondName = $_GET['SecondName'];
$AddressLine1 = $_GET['AddressLine1'];
$AddressLine2 = $_GET['AddressLine2'];
$AddressLine3 = $_GET['AddressLine3'];
$PostCode = $_GET['PostCode'];
$PhoneNumber = $_GET['PhoneNumber'];
$EMail = $_GET['EMail'];
$IdealPrice = $_GET['IdealPrice'];
$IdealLocation = $_GET['IdealLocation'];
$IdealNumberOfBedrooms = $_GET['IdealNumberofBedrooms'];
$IdealSize = $_GET['IdealSize'];


echo "<br>";
echo "<label for='FirstName'>First Name*:</label><br>";
echo "<input id='FirstName' type='text' maxlength='20' name='FirstName' value='$row[FirstName]' pattern='^[a-zA-Z]{2,20}$' required /><br>";
echo "<br>";
echo "<label for='SecondName'>Second Name*:</label><br>";
echo "<input id='SecondName' type='text' maxlength='20' name='SecondName' value='$row[SecondName]' pattern='^[a-zA-Z]{2,20}$' required /><br>";
echo "<br>";
echo "<label for='AddressLine1'>Address Line 1*:</label><br>";
echo "<input id='AddressLine1' type='text' maxlength='30' name='AddressLine1' value='$row[AddressLine1]' required /><br>";
echo "<br>";
echo "<label for='AddressLine2'>Address Line 2*:</label><br>";
echo "<input id='AddressLine2' type='text' maxlength='30' name='AddressLine2' value='$row[AddressLine2]' required /><br>";
echo "<br>";
echo "<label for='AddressLine3'>Address Line 3*:</label><br>";
echo "<input id='AddressLine3' type='text' maxlength='30' name='AddressLine3' value='$row[AddressLine3]' required /><br>";
echo "<br>";
echo "<label for='PostCode'>Post Code (with a space)*:</label><br>";
echo "<input id='PostCode' type='text' maxlength='10' name='PostCode' value='$row[PostCode]' pattern='[A-Za-z]{1,2}[0-9Rr][0-9A-Za-z]? [0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}' required /><br>";
echo "<br>";
echo "<label for='PhoneNumber'>Phone Number*:</label><br>";
echo "<input id='PhoneNumber' type='tel' maxlength='11' name='PhoneNumber' value='$row[PhoneNumber]' pattern='^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$' required /><br>";
echo "<br>";
echo "<label for='EMail'>Email Address*:</label><br>";
echo "<input id='EMail' type='email' maxlength='50' name='EMail' value='$row[EMail]' required /><br>";
echo "<br>";
echo "<label for='IdealLocation'>Ideal Location:</label><br>";
echo "<input id='IdealLocation' type='text' maxlength='10' value='$row[IdealLocation]' name='IdealLocation' /><br>";
echo "<br>";
echo "<label for='IdealPrice'>Ideal Price:</label><br>";
echo "<input id='IdealPrice' type='number' min='10000' max='100000000000' value='$row[IdealPrice]' step='100' maxlength='12' name='IdealPrice' pattern='^\\$?(([1-9](\\d*|\\d{0,2}(,\\d{3})*))|0)(\\.\\d{1,2})?$' required /><br>";
echo "<br>";
echo "<label for='IdealNumberofBedrooms'>Ideal Number of Bedrooms:</label><br>";
echo "<input id='IdealNumberofBedrooms' type='number' max='10' maxlength='10' min='1' value='$row[IdealNumberofBedrooms]' name='IdealNumberofBedrooms' step='1' value='1' /><br>";
echo "<br>";
echo "<label for='IdealSize'>Ideal Size (M2):</label><br>";
echo "<input id='IdealSize' type='number' max='100000' maxlength='10' min='20' step='10' value='$row[IdealSize]' name='IdealSize' step='1' value='1' /><br>";
echo "<br>";
echo "<input id='Submit' type='submit' name='savechanges' value='Submit Changes' /><br>";
}
}






if (isset($_POST['savechanges']))
{
$FirstName = $_POST['FirstName'];
$SecondName = $_POST['SecondName'];
$AddressLine1 = $_POST['AddressLine1'];
$AddressLine2 = $_POST['AddressLine2'];
$AddressLine3 = $_POST['AddressLine3'];
$PostCode = $_POST['PostCode'];
$PhoneNumber = $_POST['PhoneNumber'];
$EMail = $_POST['EMail'];
$IdealPrice = $_POST['IdealPrice'];
$IdealLocation = $_POST['IdealLocation'];
$IdealNumberOfBedrooms = $_POST['IdealNumberofBedrooms'];
$IdealSize = $_POST['IdealSize'];


$error;


$addcustomer = "SELECT * FROM CustomerTable WHERE EMail = '$EMail'";
$resultaddcustomer = mysql_query($addcustomer);
$count = mysql_num_rows($resultaddcustomer);


if ($count == 1) {
$_POST["EMail"] = $EMail;


echo 'Sorry the Email Address you entered already has an account. Press your browsers back button to return. ';


}


else
{ 
 # Validate First Name #
        // if its not alpha numeric, throw error
        if (!ctype_alpha($FirstName)) { 
            $error .= '<p>First name should be letters only.</p>';
            echo $error;
        }
        // if first_name is not 3-20 characters long, throw error
        if (strlen($FirstName) < 3 OR strlen($FirstName) > 20) {
           $error .= '<p>First name should be within 3-20 characters long.</p>';
           echo $error;
        }


    # Validate Last Name #
        // if its not alpha numeric, throw error
        if (!ctype_alpha($SecondName)) { 
            $error .= '<p>Last name should be letters only.</p>';
           echo $error;
        }
        // if last_name is not 3-20 characters long, throw error
        if (strlen($SecondName) < 3 OR strlen($SecondName) > 20) {
            $error .= '<p>Last name should be within 3-20 characters long.</p>';
            echo $error;
        }




    # Validate Email #
        // if email is invalid, throw error
        if (!filter_var($EMail, FILTER_VALIDATE_EMAIL)) { // you can also use regex to do same
            echo $error .= '<p>Enter a valid email address.</p>';
        }


    # Validate Phone #
        // if phone is invalid, throw error
        if (!ctype_digit($PhoneNumber) OR strlen($PhoneNumber) != 11) {
            $error .= '<p>Enter a valid phone number.</p>';
            echo $error;
        }


if (empty($error)){


$queryuploadcustomer = "UPDATE CustomerTable (FirstName, SecondName, AddressLine1, AddressLine2, AddressLine3, PhoneNumber, PostCode, EMail, IdealPrice, IdealLocation, IdealNumberofBedrooms, IdealSize) VALUES ('$FirstName', '$SecondName', '$AddressLine1', '$AddressLine2', '$AddressLine3', '$PhoneNumber', '$PostCode', '$EMail', '$IdealPrice', '$IdealLocation', '$IdealNumberOfBedrooms', '$IdealSize')";


}


}
}
}
}
Edited by Ch0cu3r
Link to comment
https://forums.phpfreaks.com/topic/286405-editing-a-users-record/
Share on other sites

The way you are trying to print the value from $row is like this:

 

$row[FirstName]

It needs to be like this:

 

$row['FirstName']

 

Notice the quotes. Easy to miss that because you have a tonne of single and double quotes going on in your code.

 

Give that a try and see if it helps.

 

Denno

Try this then:

echo "<input id='FirstName' type='text' maxlength='20' name='FirstName' value='".$row['FirstName']."' pattern='^[a-zA-Z]{2,20}$' required /><br>";

Oh and don't change all of your input fields, just change one, until you get that one working, then apply the same changes to the rest. Will save you a tonne of time debugging

Denno

Edited by denno020

Ok I've made some changes since I notice a issue with my SQL Query but it still doesn't work. I get the same issue. The $row = Null according to the var dump. 

 

The new code is below.

 


 
 
 
 
if (isset($_POST['showcatalogue']))
{
$showallcustomers = "SELECT * from CustomerTable";
$allcustomerresults = mysql_query($showallcustomers);
echo"<div class='page-restrict-output'>";
echo "<form method='post' action='customer-viewer'>";
echo "<input id='editcustomerbutton' type='Submit' name='editcustomerbtn' value='Edit Customer' /><br><br /></td></tr>";
echo "Here are the results, Select only ONE Customer to edit or the operation will FAIL. <br>";
echo "<br>";
echo"<table id='selectcustomertable' border='1'>"; 
echo "<tr><th>FirstName</th><th>SecondName</th><th>Address Line 1</th><th>Address Line 2</th><th>Address Line 3</th><th>Post Code</th><th>Customer ID</th><th>Phone Number</th><th>E-Mail</th><th>Edit Customer</th></tr>"; 
while($row = mysql_fetch_array($allcustomerresults)) { 
echo "<tr><td>"; 
echo $row['FirstName']; 
echo "</td><td>";
echo $row['SecondName'];  
echo "</td><td>";
echo $row['AddressLine1'];  
echo "</td><td>";
echo $row['AddressLine2'];  
echo "</td><td>";
echo $row['AddressLine3'];  
echo "</td><td>";
echo $row['PostCode'];  
echo "</td><td>";
echo $row['CustomerID'];  
echo "</td><td>";
echo $row['PhoneNumber'];  
echo "</td><td>";
echo $row['EMail'];  
echo "</td><td>";
echo $row['EditCustomer']; 
echo "<input id='selectcustomerbutton' type='checkbox' name='selectedCustomerIDS[]' value='$row[CustomerID]'  /></td></tr>";
echo" </form>";
echo "</div>";


}
$_POST['selectedCustomerIDS'];
{
echo "<form id=alleditcustomerform action='customer-viewer' autocomplete='on' method='post' onsubmit='return(validatecustomer());' >";
echo "<input type='hidden' name='savechanges' value='true' />";
echo "<br>";
foreach($_POST['selectedCustomerIDS'] as $CustomerIDforedit){
echo " You have selected Customer Number $CustomerIDforedit";
$theselectedrow = "SELECT * from CustomerTable WHERE 'CustomerID' = '$CustomerIDforedit'";
$theselectedrowresults = mysql_query($theselectedrow);
$count = mysql_num_rows($theselectedrowresults);


if ($count == 1) {


$FirstName = $_GET['FirstName'];
$SecondName = $_GET['SecondName'];
$AddressLine1 = $_GET['AddressLine1'];
$AddressLine2 = $_GET['AddressLine2'];
$AddressLine3 = $_GET['AddressLine3'];
$PostCode = $_GET['PostCode'];
$PhoneNumber = $_GET['PhoneNumber'];
$EMail = $_GET['EMail'];
$IdealPrice = $_GET['IdealPrice'];
$IdealLocation = $_GET['IdealLocation'];
$IdealNumberOfBedrooms = $_GET['IdealNumberofBedrooms'];
$IdealSize = $_GET['IdealSize'];


echo "<br>";
echo "<label for='FirstName'>First Name*:</label><br>";
echo "<input id='FirstName' type='text' maxlength='20' name='FirstName' '".$row['FirstName']."' pattern='^[a-zA-Z]{2,20}$' required /><br>";
echo "<br>";
echo "<label for='SecondName'>Second Name*:</label><br>";
echo "<input id='SecondName' type='text' maxlength='20' name='SecondName' value='$row[SecondName]' pattern='^[a-zA-Z]{2,20}$' required /><br>";
echo "<br>";
echo "<label for='AddressLine1'>Address Line 1*:</label><br>";
echo "<input id='AddressLine1' type='text' maxlength='30' name='AddressLine1' value='$row[AddressLine1]' required /><br>";
echo "<br>";
echo "<label for='AddressLine2'>Address Line 2*:</label><br>";
echo "<input id='AddressLine2' type='text' maxlength='30' name='AddressLine2' value='$row[AddressLine2]' required /><br>";
echo "<br>";
echo "<label for='AddressLine3'>Address Line 3*:</label><br>";
echo "<input id='AddressLine3' type='text' maxlength='30' name='AddressLine3' value='$row[AddressLine3]' required /><br>";
echo "<br>";
echo "<label for='PostCode'>Post Code (with a space)*:</label><br>";
echo "<input id='PostCode' type='text' maxlength='10' name='PostCode' value='$row[PostCode]' pattern='[A-Za-z]{1,2}[0-9Rr][0-9A-Za-z]? [0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}' required /><br>";
echo "<br>";
echo "<label for='PhoneNumber'>Phone Number*:</label><br>";
echo "<input id='PhoneNumber' type='tel' maxlength='11' name='PhoneNumber' value='$row[PhoneNumber]' pattern='^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$' required /><br>";
echo "<br>";
echo "<label for='EMail'>Email Address*:</label><br>";
echo "<input id='EMail' type='email' maxlength='50' name='EMail' value='$row[EMail]' required /><br>";
echo "<br>";
echo "<label for='IdealLocation'>Ideal Location:</label><br>";
echo "<input id='IdealLocation' type='text' maxlength='10' value='$row[IdealLocation]' name='IdealLocation' /><br>";
echo "<br>";
echo "<label for='IdealPrice'>Ideal Price:</label><br>";
echo "<input id='IdealPrice' type='number' min='10000' max='100000000000' value='$row[IdealPrice]' step='100' maxlength='12' name='IdealPrice' pattern='^\\$?(([1-9](\\d*|\\d{0,2}(,\\d{3})*))|0)(\\.\\d{1,2})?$' required /><br>";
echo "<br>";
echo "<label for='IdealNumberofBedrooms'>Ideal Number of Bedrooms:</label><br>";
echo "<input id='IdealNumberofBedrooms' type='number' max='10' maxlength='10' min='1' value='$row[IdealNumberofBedrooms]' name='IdealNumberofBedrooms' step='1' value='1' /><br>";
echo "<br>";
echo "<label for='IdealSize'>Ideal Size (M2):</label><br>";
echo "<input id='IdealSize' type='number' max='100000' maxlength='10' min='20' step='10' value='$row[IdealSize]' name='IdealSize' step='1' value='1' /><br>";
echo "<br>";
echo "<input id='Submit' type='submit' name='savechanges' value='Submit Changes' /><br>";
}
}






if (isset($_POST['savechanges']))
{
$FirstName = $_POST['FirstName'];
$SecondName = $_POST['SecondName'];
$AddressLine1 = $_POST['AddressLine1'];
$AddressLine2 = $_POST['AddressLine2'];
$AddressLine3 = $_POST['AddressLine3'];
$PostCode = $_POST['PostCode'];
$PhoneNumber = $_POST['PhoneNumber'];
$EMail = $_POST['EMail'];
$IdealPrice = $_POST['IdealPrice'];
$IdealLocation = $_POST['IdealLocation'];
$IdealNumberOfBedrooms = $_POST['IdealNumberofBedrooms'];
$IdealSize = $_POST['IdealSize'];


$error;


$addcustomer = "SELECT * FROM CustomerTable WHERE EMail = '$EMail'";
$resultaddcustomer = mysql_query($addcustomer);
$count = mysql_num_rows($resultaddcustomer);


if ($count == 1) {
$_POST["EMail"] = $EMail;


echo 'Sorry the Email Address you entered already has an account. Press your browsers back button to return. ';


}


else
{ 
 # Validate First Name #
        // if its not alpha numeric, throw error
        if (!ctype_alpha($FirstName)) { 
            $error .= '<p>First name should be letters only.</p>';
            echo $error;
        }
        // if first_name is not 3-20 characters long, throw error
        if (strlen($FirstName) < 3 OR strlen($FirstName) > 20) {
           $error .= '<p>First name should be within 3-20 characters long.</p>';
           echo $error;
        }


    # Validate Last Name #
        // if its not alpha numeric, throw error
        if (!ctype_alpha($SecondName)) { 
            $error .= '<p>Last name should be letters only.</p>';
           echo $error;
        }
        // if last_name is not 3-20 characters long, throw error
        if (strlen($SecondName) < 3 OR strlen($SecondName) > 20) {
            $error .= '<p>Last name should be within 3-20 characters long.</p>';
            echo $error;
        }




    # Validate Email #
        // if email is invalid, throw error
        if (!filter_var($EMail, FILTER_VALIDATE_EMAIL)) { // you can also use regex to do same
            echo $error .= '<p>Enter a valid email address.</p>';
        }


    # Validate Phone #
        // if phone is invalid, throw error
        if (!ctype_digit($PhoneNumber) OR strlen($PhoneNumber) != 11) {
            $error .= '<p>Enter a valid phone number.</p>';
            echo $error;
        }


if (empty($error)){


$queryuploadcustomer = "UPDATE CustomerTable (FirstName, SecondName, AddressLine1, AddressLine2, AddressLine3, PhoneNumber, PostCode, EMail, IdealPrice, IdealLocation, IdealNumberofBedrooms, IdealSize) VALUES ('$FirstName', '$SecondName', '$AddressLine1', '$AddressLine2', '$AddressLine3', '$PhoneNumber', '$PostCode', '$EMail', '$IdealPrice', '$IdealLocation', '$IdealNumberOfBedrooms', '$IdealSize')";


}


}
}
}
}
Edited by Ch0cu3r

Are you sure you're actually getting results from your SQL query?

 

Have you got phpmyadmin or something similar that you can use to test your query?

 

Also, can you post your code inside [ code ] tags, makes it much easier to read

Edited by denno020

That might be the issue, here is what I think it should be doing. The CustomerID for this test in the database is 13. If I do 

SELECT * FROM `CustomerTable` WHERE `CustomerID` = 13

in phpmyadmin. I get the result returned.

 

The ID that it taken from the form is stored in $CustomerIDforedit. I echo this out after a customer is selected and that also says 13, or whatever ID corresponds the record you have selected. So when I select from the database using

$theselectedrow = "SELECT * from CustomerTable WHERE 'CustomerID' = '$CustomerIDforedit'";

that should select the Customer ID from the database that is equal to the number stored in $CustomerIDforedit. So if $CustomerIDforedit is echoing out 13 it should select record 13.

 

 

Edited by antbates1991

Here you have not got the results from the query. You have only queried the database

$theselectedrow = "SELECT * from CustomerTable WHERE 'CustomerID' = '$CustomerIDforedit'";
$theselectedrowresults = mysql_query($theselectedrow);
$count = mysql_num_rows($theselectedrowresults);

You need to get the results from the query using one of the mysql_fetch_* functions. Eg

$row = mysql_fetch_assoc($theselectedrowresults);
// echo the form for editing record

If your query returns multiple results, then you'll need to use a while loop

while($row = mysql_fetch_assoc($theselectedrowresults))
{
    // echo form for editing record
}
Edited by Ch0cu3r

I've added this and now if I place the form in a while loop it is not echoed. Does this mean the while loop is not finding any rows for the condition? and

var_dump($row);die(); 

 shows bool(false).

Where have place that? Before or after you defined $row?

 

After.

I've done some more editing, I've changed the query to 

$theselectedrow = "SELECT * from CustomerTable WHERE 'CustomerID' = '13'";

As I know this works and selects the record in phpmyadmin. But here I get no result again just bool(false).

After.

I've done some more editing, I've changed the query to 

$theselectedrow = "SELECT * from CustomerTable WHERE 'CustomerID' = '13'";

As I know this works and selects the record in phpmyadmin. But here I get no result again just bool(false).

 

So your code is this

$theselectedrow = "SELECT * from CustomerTable WHERE 'CustomerID' = '13'";
var_dump($row);die(); 

Then, it will always be null. Because a) you have not executed the query and b) you haven't define $row.

Ok, check to make sure your query is returning an error.

$theselectedrow = "SELECT * from CustomerTable WHERE 'CustomerID' = '13'";
$theselectedrowresults = mysql_query($theselectedrow);

if(!$theselectedrowresults)
{
   echo '<p><b>Database error:</b> ' . mysql_error() . '</p>';
}
else
{
    if(mysql_num_rows($theselectedrowresults))
    {
        $row = mysql_fetch_assoc($theselectedrowresults);
        var_dump($row);
    }
    else
    {
        echo 'No results returned from query';
    }
}
Edited by Ch0cu3r
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.