Page 1 of 1

HELP??? update mysql data using php?

Posted: Tue Jan 27, 2004 5:33 pm
by tanjafei
I'm trying to make a simple little content managment page. i have a little problem with the update function - it wont update the original it males a new entry and only puts oooo-oo-oo in the date... and does not write anything in the other tables.

here is how it looks:
http://www.networkvancouver.com/try/news.php

anyone able to help me? thanks...

Code: Select all

<?
@$db=mysql_connect("***","****","****");
mysql_select_db("*******");


if($submit=="submit")&#123;
$query="insert into news values('','".$title."','".$date."','".$text1."','".$text2."')";
mysql_query($query); 
header('Location: news.php'); 
&#125;
///////////////////finished inserting

/////////////////////////start delete - update
if ($action=="update")&#123;
	$query="Update news set id, title, date, text1, text2 from news where id=".$id;
	echo($query);
	$result=mysql_query($query);
    $output2=mysql_fetch_array($result);
	mysql_query($query); 

	
&#125;
if ($action=="delete")&#123;
	$query="delete from news where id=".$id;
	echo($query);
	mysql_query($query);
&#125;

/////////////////////////end delete - update


//   set the select statement
$query="select id, title, date, text1, text2 from news";
$result_quiz=mysql_query($query);

//   execute the query on the database and put it in the variable $result
$result=mysql_query($query);	

//   find out how many rows will be returned
$rows=mysql_num_rows($result);

//	 new table
echo("<table border='1'>");

echo ("<tr>
	<td>Id</td>
	<td>Title</td>
	<td>Date</td>
	<td>Text 1</td>
	<td>Text 2</td>
	</tr>");

//    loop throught the results
for ($i=0;$i<$rows;$i++)&#123;
	$output=mysql_fetch_array($result);
	
	echo ("<tr>");
	echo ("<td>".$output&#1111;'id']."</td>");
	echo ("<td>".$output&#1111;'title']."</td>");
	echo ("<td>".$output&#1111;'date']."</td>");
	echo ("<td>".$output&#1111;'text1']."</td>");
	echo ("<td>".$output&#1111;'text2']."</td>");
	echo ("<td><a href='news.php?id=".$output&#1111;'id']."&action=update'>Update</a></td>");
	echo ("<td><a href='news.php?id=".$output&#1111;'id']."&action=delete'>Delete</a></td>");
	echo ("</tr>");
&#125;
echo("</table>");
?>

<!-- this is the form for the update -->
<br>
<form>
	<b>Please upate:</b><br>
	Title:&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="question" size="80" value="<? echo($output2&#1111;'title']);?>"><br>
	Date:&nbsp;&nbsp;&nbsp;<input type="text" name="correct" size="30" value="<? echo($output2&#1111;'date']); ?>"><br>
	Text 1:&nbsp;<input type="text" name="wrong1" size="30" value="<? echo($output2&#1111;'text1']); ?>"><br>
	Text 2:&nbsp;<input type="text" name="wrong2" size="30" value="<? echo($output2&#1111;'text2']); ?>"><br>
<input type='submit' name='submit' value='submit'>
</form>

Posted: Tue Jan 27, 2004 6:35 pm
by jason
Well, of course. You have your if checking if $submit equals submit, and your update checking if $action equals update. In your form, you only have submit equalling submit, and now action.

That's where your problem is.

As far as your date is concerned. You would be better off using the MySQL function NOW(), and that will insert the date automatically for you, so you don't have to manually do it yourself.

Posted: Tue Jan 27, 2004 7:24 pm
by tanjafei
i have this line which lets it know that the "action=update":

echo ("<td><a href='news.php?id=".$output['id']."&action=update'>Update</a></td>");