Jump to content

SQL Update


jaybeeb

Recommended Posts

Having problems trying to update a sql table. The first part of code is where the user edits the actual data. It brings the data in just fine i.e when this page opens the data to be edited is in the correct text boxes, but when data is changed and update is clicked, it goes back to the table gives no errors - but the data has not been edited.

 

Any ideas?

 

Thanks in advance,

 

<?php
require_once 'library/db.php';
//require_once 'error.php';



if (!($conn = mysql_connect('localhost', 'root', '')))
{
	showError();
}

if (!(mysql_select_db('emp_agency', $conn)))
{
	showError();
}

$a = $_GET['id'];

if (!($result = mysql_query("select * from jobs where id = '$a'", $conn)))
{
	$conn = mysql_connect('localhost', 'root', '') OR DIE (mysql_error());
}

$row =mysql_fetch_array($result); 
mysql_close($conn);


?>

<html>
<body>

<form action="saveedit.php" method="post">
<table border="0">
	<tr>
		<td>
			ID
		</td>
		<td>
			<?php echo $row['id']; ?>
			<input type="hidden" name="User_ID" value = "<?php echo $row['id']; ?>">
		</td>
	</tr>


	<tr>
		<td>
			Job Description
		</td>
		<td>
			<input type="text" name="job_description" value = "<?php echo $row['job_description']; ?>">
		</td>
	</tr>

        <tr>
		<td>
			Experience Required
		</td>
		<td>
			<input type="integer" name="experience_required" value = "<?php echo $row['experience_required']; ?>">
		</td>
	</tr>

	 <tr>
		<td>
			Contact
		</td>
		<td>
			<input type="text" name="contact" value = "<?php echo $row['contact']; ?>">
		</td>
	</tr>



</table>
<input type="submit" value="Update">
</form>
</body>
</html>

 

<?php
require_once 'library/db.php';
//require_once 'error.php';

$User_ID = $_GET['id'];

if (!($conn = mysql_connect('localhost', 'root', '')))
{
	showError();
}

if (!(mysql_select_db('itsupport', $conn)))
{
	showError();
}


if (!($result = mysql_query("UPDATE jobs SET 
job_description=\"$_POST[job_description]\",
experience_required=\"$_POST[experience_required]\",
    contact=\"$_POST[contact]\"
    
    
    WHERE id=\"$_POST[id]\"",$conn)))



{
	$conn = mysql_connect('localhost', 'root', '') OR DIE (mysql_error());
}

mysql_close($conn);
include 'employerpage.php';
?>

Link to comment
https://forums.phpfreaks.com/topic/128411-sql-update/
Share on other sites

Use mysql_error() after your query to see why it's failing.

 

Your query is incorrect

Sorry , bit of a PHP amateur. I tried putting echo mysql_error(); after the query but doesnt display an error.

 

Also, what is incorrect about my query?

 

Thanks for the help.

 

It's better to connect to the db before calling the query.

 

require_once 'library/db.php'; is connecting to the db

<?php
// database configuration
$db = array('Server' => 'localhost', 'user' => 'root', 'password' => '', database => 'emp_agency');
?>

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/128411-sql-update/#findComment-665348
Share on other sites

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.