i do believe my code in update is wrong because saving it is working but having trouble with the "Edit". help anyone?

<?php

include 'conn.php';

$flag=$_GET["flag"];

if ($flag=="1")
{
    $id = $_GET["id"];
    $lname = $_GET["lname"];
    $fname = $_GET["fname"];
    $mname = $_GET["mname"];
    $age = $_GET["age"];
    $sex = $_GET["sex"];
    $course = $_GET["course"];


    $edit = mysql_query( "UPDATE newbase2 SET  lname = '".$lname."', fname = '".$fname."', mname = '".$mname."', age='".$age.", 'gender = '".$txtgender."', 

course = '".$course."'  WHERE  id = '$id' ");
    $nres = mysql_query($edit);
    if(!$nres) {
        echo " Failed to edit: ".mysql_error();
    } else {
        echo " Editing successful";
    }
}



if ($flag=="2")
{
    $id = $_POST["id"];
    $lname = $_POST["lname"];
    $fname = $_POST["fname"];
    $mname = $_POST["mname"];
    $age = $_POST["age"];
    $sex = $_POST["sex"];
    $course = $_POST["course"];

    $ans = "INSERT INTO newbase2 VALUES ('$id','$lname', '$fname', '$mname' , '$age' , '$sex', '$course')";
    $result = mysql_query($ans);

    if($result!="") 
	{
        echo "<br>Saving Successful";
    	} 
	else 
	{
        echo "<br>Failed". mysql_error();
    	}
}

?>

<html>
<body>
<form method = "post" action = "newedit.php?flag=2">
<pre>
<font size = 5><b>EDITING INFO</b></font>


<b>NAME:</b> <input type = "text" name = "lname" size = "25" maxlength = "20" value = "<?php echo $lname; ?>"> <input type = "text" name = "fname" size = 

"25" maxlength = "20" value = "<?php echo $fname; ?>"> <input type = "text" name = "mname" size = "3" maxlength = "3" value = "<?php echo $mname; ?>">
      Lastname               Firstname              Middle
<b>AGE:  </b><input type = "text" name = "age" size = "3" maxlength = "3" value = "<?php echo $age; ?>">
<b>SEX: </b><input type = "radio" name = "sex" value = "M"> MALE <input type = "radio" name = "sex" value = "F"> FEMALE
<table>
<b>COURSE:</b> <tr><td><input type = "radio" name = "course" value = "CBA">CBA</td><td><input type = "radio" name = "course" value = "CEA">CEA</td><td><input 

type = "radio" name = "course" value = "CICT">CICT</td></tr><tr><td><input type = "radio" name = "course" value = "CAS">CAS</td><td><input type = "radio" 

name = "course" value = "CHM">CHM</td><td><input type = "radio" name = "course" value = "CON">CON</td></tr>
</table>
<input type = "submit" name = "submit" value = "SAVE"><input type = "reset" name = "clear" value = "CANCEL">
</form>
</pre>

<?php
$result2 = mysql_query("SELECT * FROM newbase2") or die("Error: " . mysql_error());
echo '<table border ="1">';
echo '<th>ACTION</th><th>ID</th><th>NAME</th><th>AGE</th><th>SEX</th><th>COURSE</th>';
if (count($result2)>0)
{
    while ($a = mysql_fetch_array($result2))
    {
        echo "<tr>";
        echo "<td><a href=\"newedit.php?id=".$a["id"]."&flag=1\"> Edit </a></td>";
	echo "<td>".$a["id"]."</td>";
        echo "<td>".$a["lname"].", ".$a["fname"]." ".$a["mname"].".</td>";
	echo "<td>".$a["age"]."</td>";
        echo "<td>".$a["sex"]."</td>";
        echo "<td>".$a["course"]."</td>";
        echo "</tr>";
    }
}
echo "</table>";

?>

</body>
</html>

There are a few points you need to adjust in your HTML

>> You should stop using spaces between attributes and their values
>> The input tag needs to be self-closed

Correct usage example: <input type="radio" name="course" value="CBA" /> And then there are a few points you need to improve in your PHP code:

>> Line 5 - You are not yet certain that the variable 'flag' is set, so you should use the following:

if (isset($_GET['flag'])) {
$flag = htmlentities(addslashes($_GET['flag'])); // Removing all bad things like HTML and quotes
} else {
// Default value:
$flag = -1;
}

>> Line 9-15 and 33-39 - You need to clean the retrieved data before executing it, as people can mess up you query by using quotes:

$variable = htmlentities(addslashes($_POST['variable']));

You can find other tips for programming here: http://www.webdevproject.com/guides/programming/guide2.html

You also did not tell us what went wrong with your code, because at first glance, it looks alright and it is practically well written.

~G

<form action = "newedit.php?flag=2">
you may remove method="" in order $_GET to work.
If you do not want, you may make $_GET as $_POST and

<form method = "post" action = "newedit.php">
<input name="flag" type="hidden" value="2">
// you may place submit button and other fields
</form>

my code accept inputted items but whenever i selectecho "<td><a href=\"newedit.php?id=".$a["id"]."&flag=1\"> Edit </a></td>"; an error occurs Failed to edit: Query was empty. what should i do?

You are running the query twice. Once on line 18 and again on 21. Remove line 21, edit variable names to reflect changes and it should work.

A future error on line 18:
mysql_query( "UPDATE newbase2 SET lname = '".$lname."', fname = '".$fname."', mname = '".$mname."', age='".$age.", 'gender = '".$txtgender."',

course = '".$course."' WHERE id = '$id' ");

mysql_query( "UPDATE newbase2 SET lname = '".$lname."', fname = '".$fname."', mname = '".$mname."', age='".$age.", 'gender = '".$txtgender."', course = '".$course."' WHERE id = '$id' ");

can be more simple as:

mysql_query( "UPDATE newbase2 SET lname = '$lname', fname = '$fname', mname = '$mname', age='$age', gender = '$txtgender', course = '$course' WHERE id = $id");

just updated my code, but still im encountering this error Duplicate entry '1' for key 'PRIMARY'
, what should i do?

<?php

include 'conn.php';

$flag=$_GET["flag"];

if ($flag=="1")
{
    $id = $_GET["id"];
    $lname = $_GET["lname"];
    $fname = $_GET["fname"];
    $mname = $_GET["mname"];
    $age = $_GET["age"];
    $sex = $_GET["sex"];
    $course = $_GET["course"];

$sql = "UPDATE `newbase2` SET `fname` = '$fname',`lname` = '$lname', `mname` = '$mname',`age` = '$age', `sex` = '$sex',`course` = '$course' WHERE 

`newbase2`.`id` = '$id' ";mysql_query($sql) or die ("Error: ".mysql_error());
echo "Updated Database.";
             
        
}



if ($flag=="2")
{
    $id = $_POST["id"];
    $lname = $_POST["lname"];
    $fname = $_POST["fname"];
    $mname = $_POST["mname"];
    $age = $_POST["age"];
    $sex = $_POST["sex"];
    $course = $_POST["course"];

    $ans = "INSERT INTO newbase2 VALUES ('$id','$lname', '$fname', '$mname' , '$age' , '$sex', '$course')";
    $result = mysql_query($ans);

    if($result!="") 
    {
        echo "<br>Saving Successful";
        } 
    else 
    {
        echo "<br>Failed". mysql_error();
        }
}

?>

<html>
<form method = "post" action = "newedit.php?flag=2">
<pre>
<font size = 5><b>EDITING INFO</b></font>


<b>ID:</b> <input type = "text" name = "id" size = "25" maxlength = "8" value = "<?php echo $id; ?>">
<b>NAME:</b> <input type = "text" name = "lname" size = "25" maxlength = "20" value = "<?php echo $lname; ?>"> <input type = "text" name = "fname" size = 

"25" maxlength = "20" value = "<?php echo $fname; ?>"> <input type = "text" name = "mname" size = "3" maxlength = "3" value = "<?php echo $mname; ?>">
      Lastname               Firstname              Middle
<b>AGE:  </b><input type = "text" name = "age" size = "3" maxlength = "3" value = "<?php echo $age; ?>">
<?php
if($sex=='M')
$flag = 'checked';
else
$flag2 = 'checked';
?>
<b>SEX: </b><input type = "radio" name = "sex" value = M "<?php echo $flag;?>"> MALE <input type = "radio" name = "sex" value =F "<?php echo $flag2;?>"> 

FEMALE
<?php
if($sex=='CBA')
$course = 'checked';
else if ($sex=='CEA')
$course2 = 'checked';
else if ($sex=='CICT')
$course3 = 'checked';
else if ($sex=='CAS')
$course4 = 'checked';
else if ($sex=='CHM')
$course5 = 'checked';
else
$course6 = 'checked';
?>
<table>
<b>COURSE:</b> <tr><td><input type = "radio" name = "course" value = CBA "<?php echo $course;?>">CBA</td><td><input type = "radio" name = "course" value = 

CEA "<?php echo $course2;?>">CEA</td><td><input type = "radio" name = "course" value = CICT "<?php echo $course3;?>">CICT</td></tr><tr><td><input type = 

"radio" name = "course" value = CAS "<?php echo $course4;?>">CAS</td><td><input type = "radio" name = "course" value = CHM "<?php echo 

$course5;?>">CHM</td><td><input type = "radio" name = "course" value = CON "<?php echo $course6;?>">CON</td></tr>
</table>
<input type = "submit" name = "submit" value = "SAVE"><input type = "reset" name = "clear" value = "CANCEL">
</form>
</pre>

<?php
$result2 = mysql_query("SELECT * FROM newbase2") or die("Error: " . mysql_error());
echo '<table border ="1">';
echo '<th>ACTION</th><th>ID</th><th>NAME</th><th>AGE</th><th>SEX</th><th>COURSE</th>';
if (count($result2)>0)
{
    while ($a = mysql_fetch_array($result2))
    {
        echo "<tr>";
        echo "<td><a href=\"newedit.php?id=".$a["id"]."&flag=1\"> Edit </a></td>";
    echo "<td>".$a["id"]."</td>";
        echo "<td>".$a["lname"].", ".$a["fname"]." ".$a["mname"].".</td>";
    echo "<td>".$a["age"]."</td>";
        echo "<td>".$a["sex"]."</td>";
        echo "<td>".$a["course"]."</td>";
        echo "</tr>";
    }
}
echo "</table>";

?>

</body>
</html>

That's a mysql error saying that as you were trying to insert a duplicate entry into the database with, I'm assuming, an ID of 1 that is also set to auto_increment.

If this is the case, don't bother trying to set the ID of the row yourself unless you're trying to rename/reorder them, line 37. Auto_increment will, as the name itself explains, increment itself. For example, if you have 3 rows in the database and you go to add another, it will automatically define the ID for the new row as 4 and so forth.

[Links]
http://www.plus2net.com/sql_tutorial/mysql_auto_increment.php
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

so what should i do?

Quite simple really (refer to bolded text in quote).

That's a mysql error saying that as you were trying to insert a duplicate entry into the database with, I'm assuming, an ID of 1 that is also set to auto_increment.

If this is the case, don't bother trying to set the ID of the row yourself unless you're trying to rename/reorder them, line 37. Auto_increment will, as the name itself explains, increment itself. For example, if you have 3 rows in the database and you go to add another, it will automatically define the ID for the new row as 4 and so forth.

[Links]
http://www.plus2net.com/sql_tutorial/mysql_auto_increment.php
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

My apologies, I did not read the syntax of your query very well. Using that syntax you must include all fields, including ID, which you have done.

What I'd recommend doing is making the input field within the page a readonly field and when the page loads, grab the number of rows from within the database already, add one and put that in as the value for the ID within the form. That way when it's submitted, the newest ID will be within the form and inserted into the database.

The only problem I can currently foresee would be if you deleted a few rows, as that would change the next auto-incremented value and would cause the duplicate key issue again.

As a possible solution to look into, maybe you should think about using the syntax:

/* Note the addition of the column names. THESE MUST BE CHANGED TO REFLECT THE ACTUAL COLUMN NAMES
   Note that the variable $id was removed from the query */
INSERT INTO newbase2 (`last_name`,`first_name`,`middle_name`,`age`,`sex`,`course`) VALUES ('$lname', '$fname', '$mname' , '$age' , '$sex', '$course')

That way you can let the database itself create and insert the ID as you add more rows, etc...

sorry can't really understand english very well, how will i include this in my code again?

Line 41

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.