Jump to content

Error while trying to update via MYSQL


jaybeeb

Recommended Posts

Here I am clicking on a row of a table and selecting this php file underneath where I can edit particular details from whichever record I choose. "User_ID" is the primary key so this remains the same and everything else should be able to update.

 

But I keep getting this error message

 

Fatal error: Call to undefined function showError() in C:\wamp\www\NEW\selectedit.php on line 21

 

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



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

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

$a = $_GET['User_ID'];

if (!($result = mysql_query("select * from contacts where User_ID = '$a'", $conn)))
{
	showError();      //<<<<--------LINE 21;
}

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


?>

<html>
<body>
<p>
	Editing Details: <?php echo $row['User_Name']; ?>
<p>
<form action="saveedit.php" method="post">
<table border="0">
	<tr>
		<td>
			User ID
		</td>
		<td>
			<?php echo $row['User_ID']; ?>
			<input type="hidden" name="User_ID" value = "<?php echo $row['User_ID']; ?>">
		</td>
	</tr>

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

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

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

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

 

Any help would be appreciated!

Link to comment
https://forums.phpfreaks.com/topic/100540-error-while-trying-to-update-via-mysql/
Share on other sites

it means that you don't have a function called showError() anywhere in your code.

 

change line 21 to:

 

echo mysql_error();

 

OR  create a function called showError();

 

<?php
//put this near the top of your script
function showError(){
echo "Oh snap, the SQL query screwed up.  Here is last error that PHP received: ";
echo mysql_error();
}

?>

Thanks, that works now. One other thing, when I click on the record to update now I get a warning

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\selectedit.php on line 24

 

The form appears but it doesnt update, I wanted when I click update, my new form to open but to have the details of the row I clicked on, entered in the text boxes so I can edit them and keep one or two if I want, have I allowed for this in my code? Sorry for the question if it is silly I am a novice at PHP

 

Thanks,

That error means that it's not getting back a valid MySQL resource. A resource holds the data returned from the query.

 

What's your code to this point? Make sure you use the code tags.

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\selectedit.php on line 24

 

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



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

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

$a = $_GET['User_ID'];

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

$row = mysql_fetch_array($result);              //<--------LINE 24
mysql_close($conn);


?>

<html>
<head>

<title>

</title>
</head>
<body>
<p>
	Editing Details: 
	<?php echo $row['User_Name']; ?>
<p>
<form action="saveedit.php" method="post">
<table border="0">
	<tr>
		<td>
			User ID
		</td>
		<td>
			<?php echo $row['User_ID']; ?>
			<input type="hidden" name="User_ID" value = "<?php echo $row['User_ID']; ?>">
		</td>
	</tr>

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

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

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

	<tr>
		<td>
			Office Number
		</td>
		<td>
			<input type="integer" name="Office_Number" value = "<?php echo $row['Office_Number']; ?>">
		</td>
	</tr>

	<tr>
		<td>
			Phone Number
		</td>
		<td>
			<input type="integer" name="Phone_Number" value = "<?php echo $row['Phone_Number']; ?>">
		</td>
	</tr>

	<tr>


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

 

Here is my code for savedit.php (This is what the form above calls when submit is pressed)

<?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 itsupport SET 
User_Name=\"$_POST[user_Name]\", 
Problem=\"$_POST[Problem]\" 
Problem_ID=\"$_POST[Problem_ID]\", 
User_email=\"$_POST[user_email]\" 
    Office_Number=\"$_POST[Office_Number]\", 
Phone_Number=\"$_POST[Phone_Number]\" 


WHERE User_ID = $_POST[user_ID] ", $conn)))



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

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

I don't really understand why you're using all these if statements and ! operators.

 

Here's how I'd lay out the code.

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

$hostname = "localhost";
$username = "root";
$password = "";
$dbname   = "itsupport";

mysql_connect($hostname, $username, $password) OR DIE ("Unable to connect.");
mysql_select_db($dbname);

$a = $_GET['User_ID'];

$sql = "SELECT * FROM `contacts` WHERE `User_ID` = '$a'";
$result = mysql_query($sql) OR DIE ("$sql<br />".mysql_error());
if (mysql_num_rows($result))
{
$row = mysql_fetch_assoc($result);
}
mysql_close();
?>
<html>
<head>
<title></title>
</head>

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

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

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

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

<tr>
<td>
	Office Number
</td>
<td>
	<input type="integer" name="Office_Number" value = "<?php echo $row['Office_Number']; ?>" />
</td>
</tr>

<tr>
<td>
	Phone Number
</td>
<td>
	<input type="integer" name="Phone_Number" value = "<?php echo $row['Phone_Number']; ?>" />
</td>
</tr>
</table>
<input type="submit" value="Save" />
</form>

</body>
</html>

 

savedit.php

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

$hostname = "localhost";
$username = "root";
$password = "";
$dbname   = "itsupport";

mysql_connect($hostname, $username, $password) OR DIE ("Unable to connect.");
mysql_select_db($dbname);

$User_ID = $_GET['id'];

$sql = "UPDATE `itsupport` SET 
`User_Name`='{$_POST['User_Name']}',
`Problem`='{$_POST['Problem']}',
`Problem_ID`='{$_POST['Problem_ID']}',
`User_email`='{$_POST['User_email']}',
`Office_Number`='{$_POST['Office_Number']}',
`Phone_Number`='{$_POST['Phone_Number']}'
WHERE `User_ID`='{$_POST['User_ID']}'";
$result = mysql_query($sql) OR DIE ("$sql<br />".mysql_error());
if (mysql_affected_rows($result))
{
// echo "<p>UPDATE successful!</p>";
}
mysql_close();
include 'currentissues.php';
?>

 

But I think the main problem is this in your savedit.php file:

<?php
$sql = "UPDATE `itsupport` SET 
`User_Name`='{$_POST['User_Name']}',
`Problem`='{$_POST['Problem']}',
`Problem_ID`='{$_POST['Problem_ID']}',
`User_email`='{$_POST['User_email']}',
`Office_Number`='{$_POST['Office_Number']}',
`Phone_Number`='{$_POST['Phone_Number']}'
WHERE `User_ID`='{$_POST['User_ID']}'";
?>

You were missing some commas between which fields you wanted to update.

 

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.