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

ok here is the code I am using:

<?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"); // selects database
				
    $membercheck = mysql_query("SELECT `id_number` FROM `members` WHERE `id_number` ='$id_number'");
    $count = mysql_num_rows($membercheck);
	   if($count!=0){                                                                                         									$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($connect);
						
	$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['fist_name'];							$result=mysql_query("SELECT * FROM `members` WHERE first_name='$x'") or die("ERROR:".mysql_error());
										
										$row=mysql_fetch_array($result,MYSQL_ASSOC); 
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
    {
    //Redirect to Registry Page.
    die ("<h3>Patient does not exist in database.<a href="register.php" title="Register this patient?">.Click here</a> to register this patient.</h3>"
    }
				
}

Second page 'update.php'...

<?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);

?>

I probably have this all wrong but I am very new to php and any help would be appreciated, below is exactly what I am trying to do...

Ideally I would like the update page to:
1. Query the patients info and store the info in a string.
2. Post the info into a form.
3. Lastly Update the new info into the table using the existing form.


Any help would be greatly appreciated... Thank you,
Dale

I forgot to add the function render_query_form(); as it is involved with the process perhaps my error in code is there but i don't think so... I believe it is in the passing of the variables from the test.php page to the update.php
But alas here is the function:

function render_query_form()
{
$output = "

    <form action='test.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>";

return $output;
}
Member Avatar for diafol

You've got a bit of spaghetti going on here mate. No need for all those connect thingies. Put this at the head of the page or place in an include file:

$db_host ="localhost";
$db_name ="cannameds";
$db_user ="root";
$db_pw ="root";
$link_id = mysql_connect($db_host, $db_user, $db_pw);
mysql_select_db($db_name, $link_id);

I've left out the error handling for clarity's sake. But check out the php online guide as to how to implement that.

Also, this is wrong:

$num = mysql_numrows($result);
//should be
$num = mysql_num_rows($result);

I take it that this is also a typo:

$x = $_GET['fist_name'];

You're closing connections and then looping with extraneous mysql functions. Keep it simple.

Look, have a good sort through this with a decent php mysql functions tutorial and come back if you're still stuck. You need a thorough rewrite to get this to work properly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.