Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add/edit records in mySQL db using PHP

Status
Not open for further replies.

cbhead

Technical User
Jul 31, 2002
31
0
0
GB
Hi,
I'm building a php page to add/edit records in a mySQL db.
I'm having a problem with the following code.
I have written in capital letters on the line above where the error occurs and the error message returned by the browser.
The mySQL db is a single table with ID auto increment(primary key).
I would appreciate any ideas.
Thanks.

<html>
<head>
<title>Outline Vacancies input test</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>
<body bgcolor=&quot;#FFFFCC&quot; text=&quot;#000000&quot;>

<?php
if($submit)
{
require( 'connect_jobs1.php' );

$sql = &quot;INSERT INTO allvacs (ID, company, job, location, salary, hours) VALUES ('','$company','$job','$location','salary','hours');&quot;;

$result = mysql_query($sql);

echo &quot;Thank you! Information entered.\n&quot;;
}
else if($update)
{
require( 'connect_jobs1.php' );

$sql = &quot;UPDATE allvacs SET ID='',company='$company',job='$job',location='$location',salary='$salary',hours='$hours',

salary='$salary' WHERE ID=$ID&quot;;

$result = mysql_query($sql);
echo &quot;Thank you! Information updated.\n&quot;;
}
else if($ID)
{
require( 'connect_jobs1.php' );

$result = mysql_query(&quot;SELECT * FROM allvacs WHERE ID=$ID&quot;,$db);

$myrow = mysql_fetch_array($result);

?>


<form method=&quot;post&quot; action=&quot;addedit.php&quot;>

<input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;<?php echo $myrow[&quot;id&quot;]?>&quot;>

**ERROR ON THIS LINE Parse error: parse error, unexpected ':', expecting ',' or ';' in /extra0/ on line 47**

company:<input type=&quot;Text&quot; name=&quot;company&quot; value=&quot;<?php echo

job:<input type=&quot;Text&quot; name=&quot;job&quot; value=&quot;<?php echo $myrow[&quot;job&quot;]?>&quot;><br>

location:<input type=&quot;Text&quot; name=&quot;location&quot; value=&quot;<?php echo $myrow[&quot;location&quot;]?>&quot;><br>

salary:<input type=&quot;Text&quot; name=&quot;salary&quot; value=&quot;<?php echo $myrow[&quot;salary&quot;]?>&quot;><br>

hours:<input type=&quot;Text&quot; name=&quot;hours&quot; value=&quot;<?php echo $myrow[&quot;hours&quot;]?>&quot;><br>


<input type=&quot;Submit&quot; name=&quot;update&quot; value=&quot;Update information&quot;></form>
<?
}
else
{
?>
<form method=&quot;post&quot; action=&quot;addedit.php&quot;>
company:<input type=&quot;Text&quot; name=&quot;company&quot;><br>
job:<input type=&quot;Text&quot; name=&quot;job&quot;><br>
location:<input type=&quot;Text&quot; name=&quot;location&quot;><br>
salary:<input type=&quot;Text&quot; name=&quot;salary&quot;><br>
hours:<input type=&quot;Text&quot; name=&quot;hours&quot;><br>
<input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Enter information&quot;></form>
<?
}
?>
</html>
 
You should take out the ID='' from the query, you dont need to update and read only field(autoincrement)
Code:
$sql = &quot;UPDATE allvacs SET company='$company',job='$job',location='$location',salary='$salary',hours='$hours',
salary='$salary' WHERE ID=$ID&quot;;

________
George, M
 
I have removed ID from the query so it looks like this:-

$sql = &quot;UPDATE allvacs SET company='$company',job='$job',location='$location',salary='$salary',hours='$hours',

salary='$salary' WHERE ID=$ID&quot;;

but I'm still getting the same error message.

Any ideas?

 
yep you forgot the ; behind the echo (and also on the other form fields)
Code:
input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;<?php echo $myrow&quot;id&quot;;?>&quot;>

 
Still no joy!
This is the code I have now after adding semi colons. I've also tried it without the square brackets but am still getting the same error back.

<form method=&quot;post&quot; action=&quot;addedit.php&quot;>

<input type=&quot;hidden&quot; name=&quot;ID&quot; value=&quot;<?php echo $myrow[&quot;ID&quot;];?>&quot;>

company:<input type=&quot;Text&quot; name=&quot;company&quot; value=&quot;<?php echo

job:<input type=&quot;Text&quot; name=&quot;job&quot; value=&quot;<?php echo $myrow[&quot;job&quot;];?>&quot;><br>

location:<input type=&quot;Text&quot; name=&quot;location&quot; value=&quot;<?php echo $myrow[&quot;location&quot;];?>&quot;><br>

salary:<input type=&quot;Text&quot; name=&quot;salary&quot; value=&quot;<?php echo $myrow[&quot;salary&quot;];?>&quot;><br>

hours:<input type=&quot;Text&quot; name=&quot;hours&quot; value=&quot;<?php echo $myrow[&quot;hours&quot;];?>&quot;><br>


<input type=&quot;Submit&quot; name=&quot;update&quot; value=&quot;Update information&quot;></form>
<?
}
else
{
?>
 
You can use this code i've made for simplify work but also i see something that could be wrong.
Code:
the line <?php echo $myrow&quot;job&quot;;?> should be
<?php echo $myrow[&quot;job&quot;];?> i think you forgot brackets around the field name.

________
George, M
 
and yes the logic of using concatation as a namefield instead of arrays is strange ??

 
PHP is a strange language... :)
You never stop learning things.

________
George, M
 
I dont knw, i'm waiting also the response.
To lazy to verify that :)

________
George, M
 
Hmm, I don't think either of this is the problem here. You do not need to add a semi-colon at the end of the echo line if right after that you close the php block (?>), so that wouldn't be a problem. I think the gone brackets are just TGML's work, since he didn't put the [code][/code] around. What baffles me is this:
Code:
company:<input type=&quot;Text&quot; name=&quot;company&quot; value=&quot;<?php echo

job:<input type=&quot;Text&quot; name=&quot;job&quot; value=&quot;<?php echo $myrow[&quot;job&quot;];?>&quot;><br>
If this is all there is to it then you have this kind of php command:

echo job:<input type=&quot;Text&quot;...

which would made php choke for sure. Is there something that got lost in the copying or is this line really incomplete?
 
Sorry about my absence for the last few days.

I appreciate all the efforts to help me with this but I'm still unable to make it work.

I admit that I am not an expert on PHP, I used the folowing tutorial to get most of the code that I am using.


It may help to look at this to find out what I am doing wrong.

It is topic 6, editing data. It is meant to create a page called addedit.php that I can use to add, delete and ammend records.

The part that is not working should display a record from the db and then allow the user to edit it.

Also, it may be useful to know that the code I copied and pasted in earlier threads did not show square brackets around the field names that I included.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top