View Single Post
Old 07-12-2010, 10:55 PM   #1 (permalink)
dalelandrydotcom
The Visitor
Newcomer 
 
Join Date: Jul 2010
Location: San Diego
Posts: 1
Thanks: 0
dalelandrydotcom is on a distinguished road
Confused Updating MySql database table with PHP

Hello, I am new to php code (month or so) and mysql, I am pretty much teaching myself, I am good with HTML, CSS, JS, etc...


I am trying to create a DB for a client that is fairly simple; Add members (got that done); Verify member and post data below form when queried (Got that done) But the update process has been giving me MAJOR headaches...
The database is as follows:

Set up a test DB on my local server...

the DB = 'cannameds
the table = 'members'

right now I am only testing the first name field which = "first_name" but also have in the table "members":
-'last_name'
-'dob_month'
-'dob_day'
-'dob_year'
-'id_number'
-'exp_month'
-'exp_day'
-'exp_month'
-'notes'

Here is the field info on the table...
id: int(11) - - - no null - no default - auto_increment
first_name: varchar(25) - utf8-general_ci
last_name: varchar(25) - utf8-general_ci
dob_month: varchar(10) - utf8-general_ci
dob_day: varchar(10) - utf8-general_ci
dob_year: varchar(10) - utf8-general_ci
id_number: varchar(50) - utf8-general_ci
exp_month: varchar(10) - utf8-general_ci
exp_day: varchar(10) - utf8-general_ci
exp_year: varchar(10) - utf8-general_ci
notes: varchar(200) - utf8-general_ci

Here is the code I was trying but it does not update but also returns no errors, also when I put the raw data into the query UPDATE line it works but when I use the variables I have assigned it does not update...

I'm sure I have this all wrong as I am so new to php:

edit.php:

PHP Code:

<?php 

include 'functions.php';

echo 
render_header();

echo 
render_nav();

echo 
render_query_form();

        
$submit $_POST['submit'];
        
        
$id_number trim(strip_tags($_POST['id_number']));
        
        if(
$submit)
            {
                
$connect mysql_connect("localhost","root","root"); 
                
mysql_select_db("cannameds"); 
                
                
                
$membercheck mysql_query("SELECT id_number FROM members WHERE id_number ='$id_number'");
                
$count mysql_num_rows($membercheck);
                
                if (
$count!=0)
                    {                                                                                                
                        
                        
                
mysql_close($connect);        
                
                
                        
$connect mysql_connect("localhost","root","root");
                                          
mysql_select_db('cannameds') or die( "ERROR: Unable to select database<br />Check to make sure you have a proper connection to your database!>");
                        
                        
$query "SELECT * FROM members WHERE id_number ='$id_number'";
                        
                        
$result mysql_query($query);
                        
$num mysql_numrows($result);
                                        
                        
mysql_close();
                        
                                                        
                                
$i=0
                                    while (
$i $num) {
                                        
                                        
$last_name mysql_result($result,$i,"last_name");
                                        
$first_name mysql_result($result,$i,"first_name");
                                        
$dob_month mysql_result($result,$i,"dob_month");
                                        
$dob_day mysql_result($result,$i,"dob_day");
                                        
$dob_year mysql_result($result,$i,"dob_year");
                                        
$id_number mysql_result($result,$i,"id_number");
                                        
$exp_month mysql_result($result,$i,"exp_month");
                                        
$exp_day mysql_result($result,$i,"exp_day");
                                        
$exp_year mysql_result($result,$i,"exp_year");
                                        
$notes mysql_result($result,$i,"notes");
                                        
                                        
                                    
                                        
$link=mysql_connect("localhost""root""root");
                                    
                                        
mysql_select_db("cannameds") or die("unable to connect");
                                        
                                        
$x $_GET['first_name'];
                                        
                                        
$result=mysql_query("SELECT * FROM `members` WHERE first_name='$x'") or die("ERROR:".mysql_error());
                                        
                                        
$row=mysql_fetch_array($result,MYSQL_ASSOC); //since it returns only 1 row.
                                                                
                                        
echo "$first_name $last_name ";
                                        echo 
'is  a member.</h2></div><br />';
                                        echo 
'<form method="POST" name="edit_form" action="update.php"';
                                        echo 
'<input size="10" class="form_field" type="hidden" value='.$x.' name="old_name" />';
                                        echo 
'Change Name? <input size="10" class="form_field" type="text" value="'.$first_name.'" name="first_name" />';
                                        echo 
'<input name="submit" type="submit" value="Update" />';
                            
                                        
                                        
                                        
                                
$i++;
                    }    
                                    

                        }
                        
                    else
                        {
                            
                            die (
"<h3>Patient does not exist in database.</h3>");
                        }
                
            }

function 
render_query_form()
    {
        
$output "
            <html>
                        <form action='edit.php' method='post'>
                        <span><strong> Edit Patients Information.<br />Please enter the patients Clinic & Dr. ID Number : </strong></span><input class='form_field' type='text' value='' name='id_number' /><br />
                        <input class='search_btn' type='submit' name='submit' value='search' />      
                        </form>
            </html>"
;
        return 
$output;
    }

Second page...
update.php:


PHP Code:

<?php 
include 'functions.php';

echo 
render_header();
echo 
render_nav();

$link=mysql_connect("localhost""root""root");

mysql_select_db("cannameds") or die("Unable to select table!".mysql_error());

$old_name=$_POST['old_name'];
$new_name=$_POST['first_name'];

// "UPDATE `cannameds`.`members` SET `first_name` = \'Dale\' WHERE `members`.`id` = 1;";
mysql_query("UPDATE `cannameds`.`members` SET `first_name` = '$new_name' WHERE first_name='$old_name';") or die("ERROR:".mysql_error());

echo 
"You have updated $new_name's information. <a href='verify.php' title='go back to the verification page!'>Click here</a> to return to the verification page!";

mysql_close($link);

?>

Any help would be thoroughly appreciated, any suggestions for better code or heck a complete rebuild of the entire structure, I just need to get this code finished 3 days banging head against the wall ARGGGGHHHHH!
dalelandrydotcom is offline  
Reply With Quote