Jump to content

a little mysql and php guidance please


ballhogjoni

Recommended Posts

I want to update or insert data into my mysql table depending on if information exists or not. Right now I can update and insert the data, but my question is how do I update data in one column of my table without updating the data of another column in the same table? I have the issue that if I submit my form without any data entered in the field on the form it will delete/update the data in my table with nothing.

 

here is my code

<?php
$First_category_title = $_POST['First_category_title'];
$First_Link_URL = $_POST['First_Link_URL'];
$First_Link_Title = $_POST['First_Link_Title'];
$view_form = $_POST['view_form'];

if (!isset($view_form)) {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Admin Only</title>
</head>
<body>
<table>
<tr>
	<td>
	<table>
		<tr>
			<td>
			<fieldset><legend>First Category Links and Titles</legend>
			<form action="" method="post">
			<table>
				<tr>
					<td>
					First Category Title:
					</td>
					<td>
					<input type="text" name="First_category_title" value=""/>
					</td>
				</tr>
				<tr>
					<td>
					First Link URL:
					</td>
					<td>
					<input type="text" name="First_Link_URL" value=""/>
					</td>
					<td>
					First Link Title:
					</td>
					<td>
					<input type="text" name="First_Link_Title" value=""/>
					</td>
				</tr>
				<tr>
					<td colspan="4" align="center">
					<input type="hidden" name="view_form" />
					<input type="submit" value="Submit" />
					</td>
				</tr>
			</table>
			</form>
			</fieldset>
			</td>
		</tr>
	</table>
	</td>
</tr>
</table>
</body>
</html>
<?php
} elseif (!empty($First_category_title) || !empty($First_Link_URL) || !empty($First_Link_Title)) {
$username="xxxx";
$password="xxxx";
$database="xxxx";

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$result = mysql_query("SELECT * FROM leftNavigation");
$num_rows = mysql_num_rows($result);

if ($num_rows) {
	mysql_query("UPDATE leftNavigation SET First_category_title = '$First_category_title',First_Link_URL = '$First_Link_URL',First_Link_Title = '$First_Link_Title'") or die(mysql_error());
	echo 'you did it';
	echo '<FORM><INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1)"></FORM>';
} else { 
	mysql_query("INSERT INTO leftNavigation (First_category_title,First_Link_URL,First_Link_Title) VALUES ('$First_category_title','$First_Link_URL','$First_Link_Title')") or die(mysql_error());
	echo 'you added the right stuff';
	echo '<FORM><INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1)"></FORM>';
}
unset($First_category_title,$First_Link_URL,$First_Link_Title);
} else {
echo 'every field was empty, please <FORM><INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1)"></FORM> and enter information into the fields';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/58354-a-little-mysql-and-php-guidance-please/
Share on other sites

This is easy if u want to update one field not all then just mention one field in ur query like:

mysql_query("UPDATE leftNavigation SET First_category_title = '$First_category_title'") or die(mysql_error());

 

This is just an example, edit it to ur desire query.

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.