Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[Mysql] Updating Tables

Experienced Elementalist
Joined
Apr 15, 2008
Messages
256
Reaction score
0
Hey im trying to make a script that will edit the news posted if the id is given:
PHP:
<?php include('includes/header.php');?>
<?php
if(isset($_REQUEST['submit']))
{
$id=$_GET['id'];
header('location:editnews.php?id='.$id);
}
elseif(!isset($_GET['id'])){
echo"<form action='' name='submit'>ID:<input type='text' name='id' value='' /><br><input type='submit' name='submit' value='Submit'><br></form>";
}
?>
<?php 
$id=$_GET['id'];
if(isset($_GET['submit2'])){
$id=$_GET['id'];
$title = $_GET['title'];
$user = $_GET['user'];
$page_body = $_GET['page_body'];
$date = date('D F j, Y, g:i a'); 
$query =mysql_query("UPDATE news SET user = '$user', title = '$title', page_body='$page_body' WHERE ID =$id")or die("Error Code #20, Cannot Update Tables!");
} 
$sql = "SELECT * FROM news WHERE ID=$id limit 1";
$services_query = mysql_query($sql);
while($services_query_data = mysql_fetch_assoc($services_query))
	{
	echo"<form action='' method='POST' name='submit2'>";
    echo "Title:<input type='text' name='title' value= ". $services_query_data['title'] . "><br>";
    echo "Message:<textarea name='page_body' id='page_body' cols='45' rows='5'>".nl2br($services_query_data['page_body'])."</textarea>";
    echo "<br />"; 
	if(!isset($_GET['id'])){
	echo"<a href='index.php?id=" . $services_query_data['ID'] . ">read more..</a><br />";
	} 
    echo"<br />";
    echo"<strong>Posted By <input type='text' name='user' value=" . $services_query_data['user'] . "> On <input type='text' name='date' value=" . $services_query_data['date'] . "></strong><br>";
	
		echo "<input type='submit' name='submit2' value='Submit'> <input type='Reset' value='Reset'>";
	echo "</form>";
	}
?>
<?php include('includes/footer.php');?>
But when ever i click on submit (on the second form) it just resets it =/
 

Joined
Feb 26, 2007
Messages
641
Reaction score
258
You are checking whether the form has been submitted or not using
PHP:
if(isset($_GET['submit2'])){
But that simply won't work (for one it isn't $_GET but $_POST, and secondly I'm not sure it works with the form name). Use something like
PHP:
if(isset($_POST['title'])){
(took a random var from the form, should work regardless)
 
Junior Spellweaver
Joined
Dec 2, 2006
Messages
198
Reaction score
0
many errors.
PHP:
echo"<form action='' name='submit'>ID:<input type='text' name='id' value='' /><br><input type='submit' name='submit' value='Submit'><br></form>"; 
}
this form doesnt even have a method. lol.

and i noticed you dont have $services_query_data made to anything.
 
Experienced Elementalist
Joined
Apr 15, 2008
Messages
256
Reaction score
0
Im a nub lol
anyways i tried this(still not working though):
editnews.php:
PHP:
<?php include('includes/header.php');?>
<?php
if(isset($_REQUEST['submit']))
{
$id=$_GET['id'];
header('location:editnews.php?id='.$id);
}
elseif(!isset($_GET['id'])){
echo"<form action='' name='submit'>ID:<input type='text' name='id' value='' /><br><input type='submit' name='submit' value='Submit'><br></form>";
}
?>
<?php 
$id=$_GET['id'];
$sql = "SELECT * FROM news WHERE ID=$id limit 1";
$services_query = mysql_query($sql);
while($services_query_data = mysql_fetch_assoc($services_query))
	{
	echo"ID <input type='text' disabled='disabled' name='id2' value=".$id.">";
	echo"<form action='editnews2.php' method='POST' name='submit2' >";
    echo "Title:<input type='text' name='title' value= ". $services_query_data['title'] . "><br>";
    echo "Message:<textarea name='page_body' id='page_body' cols='45' rows='5'>".nl2br($services_query_data['page_body'])."</textarea>";
    echo "<br />"; 
    echo"<strong>Posted By <input type='text' name='user' value=" . $services_query_data['user'] . "> On <input type='text' name='date' value=" . $services_query_data['date'] . "></strong><br>";
		echo "<input type='submit' name='submit2' value='Submit'> <input type='Reset' value='Reset'>";
	echo "</form>";
	}
?>
<?php include('includes/footer.php');?>
editnews2.php:
PHP:
<?php include("includes/header.php");?>
<?php
if(isset($_GET['editid'])){
$id=$_GET['editid'];
}
elseif(!isset($_GET['editid'])){
$id=$_REQUEST['id2'];
}
$user=$_REQUEST['user'];
$title=$_REQUEST['title'];
$page_body=$_REQUEST['page_body'];

$query =mysql_query("UPDATE news SET user = $user', title = '$title', page_body='$page_body' WHERE ID ='1'")or die(mysql_error());
?>
<?php
include("includes/footer.php");?>
and i get this:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '', title = 'title', page_body='page_bodyqqqqqqqqqqq' WHERE ID ='1'' at line 1
 
Junior Spellweaver
Joined
Dec 2, 2006
Messages
198
Reaction score
0
PHP:
<?php include("includes/header.php");?>
<?php
if(isset($_GET['editid'])){
$id=$_GET['editid'];
}
elseif(!isset($_GET['editid'])){
$id=$_REQUEST['id2'];
}
$user=$_REQUEST['user'];
$title=$_REQUEST['title'];
$page_body=$_REQUEST['page_body'];

$query =mysql_query("UPDATE news SET user = '$user', title = '$title', page_body='$page_body' WHERE ID ='1'")or die(mysql_error());
?>
<?php
include("includes/footer.php");?>
you missed a '
 
Experienced Elementalist
Joined
Apr 15, 2008
Messages
256
Reaction score
0
Got it ;)
Now it doesn't give a error but it also doesn't update the news.
 
Junior Spellweaver
Joined
Dec 2, 2006
Messages
198
Reaction score
0
You've got all your get and posts wrong.
your forms have POST, yet your trying to get information from GET.
either change your forms to GET or change all your variables to POST.
I'm pretty sure thats the problem. echo some variable with get just to make sure. =]
 

Joined
Feb 26, 2007
Messages
641
Reaction score
258
You might also want to properly indent your code, it makes it much much easier to read and edit.
 
Back
Top