/    Sign up×
Community /Pin to ProfileBookmark

problem updating database thru form

I have a script whereby i can edit the content of a database thru clicking on a link which carries a ‘ID’ over to another page. The code in this post is the code for editing for the content. The script connects sucsessfully to the database and dispalys the current database content within its textboxes, the user can type in their new content over the text and click submit to update the database. However its not working properly. When the user clicks sumit nothing happens and the database does not get updated.

[code=php]<?

if(isset($_GET[‘id’]))
{
$query = “SELECT * FROM mynews WHERE id = ‘{$_GET[‘id’]}'”;
if (!isset($_POST[“submit”]))
{

$id = $_GET[“id”]; .
$sql = “SELECT * FROM mynews WHERE id=$id”;
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);

if ($_POST[“$submit”])
{

$title = $_POST[‘title’];
$message = $_POST[‘message’];
$user = $_POST[‘user’];
$email = $_POST[’email’];

$sql = “UPDATE mynews SET title=’$title’, message=’$message’, user=’$user’, email=’$email’ WHERE id=$id”;

$result = mysql_query($sql); //run this query
echo “Information updated. <a href=test2.php>Return</a>”;
}
?>
<form action=”editcontent.php” method=”post”>
<INPUT TYPE=”TEXT” NAME=”title” VALUE=”<?php echo $myrow[“title”] ?>” SIZE=30>
//more forms down here….
<input type=”submit” name=”submit” value=”submit”>
</form>
<?

}
}

?>

[/code]

Any ideas where i might have gone wrong?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@welshApr 16.2006 — add:
[code=php]or die ('Couldnt execute query: '.$sql.' - Error: '.mysql_error());[/code]
to the $result so:
[code=php]$result = mysql_query($sql) or die ('Couldnt execute query: '.$sql.' - Error: '.mysql_error());[/code]
I also dont see a database connection but that could be elsewhere you havent shown but for your 3rd if statement:
[code=php]
if ($_POST["$submit"])
{
$id = $_POST['id'];
$title = $_POST['title'];
$message = $_POST['message'];
$user = $_POST['user'];
$email = $_POST['email'];

$sql = "UPDATE mynews SET id='$id', title='$title', message='$message', user='$user', email='$email' WHERE id=$id";

$result = mysql_query($sql); //run this query
echo "Information updated. <a href=test2.php>Return</a>";
}
[/code]

what is $_POST["$submit"] suppose to be compare or checked for?
Copy linkTweet thisAlerts:
@MegatronauthorApr 16.2006 — the db connection is above the code i posted
what is $_POST["$submit"] suppose to be compare or checked for?[/QUOTE]

that runs the code below it if submit is pressed in the form
Copy linkTweet thisAlerts:
@MegatronauthorApr 17.2006 — does anyone have any light to shine on this problem? I had an error in my origianal code which i have removed but the script is still not working.
Copy linkTweet thisAlerts:
@chazzyApr 17.2006 — you're not defining $submit before this line, from what I can tell

[code=php]

if ($_POST["$submit"])
[/code]
Copy linkTweet thisAlerts:
@MegatronauthorApr 17.2006 — so what would i need to modify in the scirpt?

i'm still finding my feet with php
Copy linkTweet thisAlerts:
@chazzyApr 17.2006 — it depends, what are you expecting to be in $submit? i think you want to just take the $ off and look at $_POST['submit'] though.
Copy linkTweet thisAlerts:
@MegatronauthorApr 21.2006 — it depends, what are you expecting to be in $submit? i think you want to just take the $ off and look at $_POST['submit'] though.[/QUOTE]

i want the submit to input the data to the database thru the form to the db. I have removed the $ from the submit and it made no odds i'm afraid.

This is a update on the code being used
[code=php][code=php]<?php include("dbconnect.php")

if(isset($_GET['id']))
{
$query = "SELECT * FROM mynews WHERE id = '{$_GET['id']}'";
if (!isset($_POST["submit"])) //if submit has not yet been pressed, do this
{

$id = $_GET["id"]; //get the id of the news article being edited. This is provided in the link to edit it.
$sql = "SELECT * FROM mynews WHERE id='$id'"; //select all columns from mynews with this id
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);

if ($_POST['$submit'])
{

$title = $_POST['title'];
$message = $_POST['message'];
$user = $_POST['user'];


$sql = "UPDATE mynews SET title='$title', message='$message', user='$user', link='$link', rss='$rss', keyword='$keyword' WHERE id='$id'";

$result = mysql_query($sql); //run this query
echo "Information updated. <a href=test2.php>Return</a>";
}
?>

<form action="editcontent.php" method="post">
<table width="400" border="0">
<tr>
<td width="109">News Title </td>
<td width="281"><INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["title"] ?>" SIZE=30></td>
</tr>
</table>
</form>


<?

}
}

?>

[/code]

i have dumped this script and found a better one.
Copy linkTweet thisAlerts:
@chazzyApr 21.2006 — do me a favor.

somewhere in your script, just so you can understand the issue here.

[code=php]
echo "Submit (with dollar sign): ".$_POST['$submit'];
echo "n<br />";
echo "Submit: ".$_POST['submit'];
[/code]


What I don't get is this:

[code=php]
if (!isset($_POST["submit"])) //if submit has not yet been pressed, do this
{

$id = $_GET["id"]; //get the id of the news article being edited. This is provided in the link to edit it.
$sql = "SELECT * FROM mynews WHERE id='$id'"; //select all columns from mynews with this id
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);

if ($_POST['$submit'])
{
[/code]

Are these two events that can happen with each other? I don't see where in your script you are defining $submit.
×

Success!

Help @Megatron spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...