problem with updating database after dropdownlist submission

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mohammedsalah
Forum Newbie
Posts: 11
Joined: Fri May 18, 2012 7:30 am

problem with updating database after dropdownlist submission

Post by mohammedsalah »

hi, im populating drop down list from database and want to update value in the table depnding on user submission.
my database named "dates" , table is "apps" columns are "app" wich stored appointments there and another column named "available" which cointain status of the appointment [yes/no] and need to be updated after user submit a specific "app" from the drop downlist.

Code: Select all

<?php
$con = mysql_connect("localhost","root","root");
$result=@mysql_query("select app from dates.apps where available='yes'");

echo"<form action='db_update.php'  method='post'>";

print "<p>select an appointment:\n";
print "<select name=appointment>\n";

while($row=mysql_fetch_assoc($result))
	{
		$appointment=$row['app'];
		
		print"<option value=$appointment/>$appointment\n";
	
	}
print"</select>\n";
echo'<input type="submit"/>';

mysql_close($con);
?>
and the db_update.php

Code: Select all

<?php

$con = mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("dates", $con);

mysql_query("update apps set available='no' where app='$_post[appointment]'");


mysql_close($con);


?>
The problem is that nothing changes in database :banghead:
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: problem with updating database after dropdownlist submis

Post by Grizzzzzzzzzz »

mohammedsalah wrote: db_update.php

Code: Select all

<?php

$con = mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("dates", $con);

mysql_query("update apps set available='no' where app='$_post[appointment]'");


mysql_close($con);


?>
should be $_POST['appointment']

additionally as a better practice, you'll want to be storing the post info in a variable & escape it before using it in a query

eg:

Code: Select all

<?php


$con = mysql_connect("localhost","root","root");
$appointment = mysql_real_escape_string($_POST['appointment']);

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("dates", $con);

mysql_query("update apps set available='no' where app='$appointment'");


mysql_close($con);


?>
mohammedsalah
Forum Newbie
Posts: 11
Joined: Fri May 18, 2012 7:30 am

Re: problem with updating database after dropdownlist submis

Post by mohammedsalah »

thank u , but still not working:(
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: problem with updating database after dropdownlist submis

Post by Grizzzzzzzzzz »

mohammedsalah wrote:thank u , but still not working:(
you also need to close the form in the first part

Code: Select all

echo'</form>';
it'll probably just be something silly like that, just have a look through it again :)
mohammedsalah
Forum Newbie
Posts: 11
Joined: Fri May 18, 2012 7:30 am

Re: problem with updating database after dropdownlist submis

Post by mohammedsalah »

thanks again , for still trying to help but,
still not working , there is no problem in form action , it goes to db_update.php i tested it before , but nothing changes in the database after user submit
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: problem with updating database after dropdownlist submis

Post by Celauran »

mohammedsalah wrote:

Code: Select all

mysql_query("update apps set available='no' where app='$_post[appointment]'");
The problem is that nothing changes in database
$_post doesn't exist. It's $_POST.
mohammedsalah
Forum Newbie
Posts: 11
Joined: Fri May 18, 2012 7:30 am

Re: problem with updating database after dropdownlist submis

Post by mohammedsalah »

yes he told me that at the first reply, still not working
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: problem with updating database after dropdownlist submis

Post by Celauran »

What error is it giving you?
mohammedsalah
Forum Newbie
Posts: 11
Joined: Fri May 18, 2012 7:30 am

Re: problem with updating database after dropdownlist submis

Post by mohammedsalah »

db_update.php dosent excute the sql query , i dont know , i htink the error is in :
1- getting the submitted variable to use
2- in the queries in db_update.php
3-that while loop , which i dont understand that function in it, just found it on the internet

, thanks for trying to help anyway :)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: problem with updating database after dropdownlist submis

Post by Celauran »

Have you checked with mysql_error() or are you just guessing?
mohammedsalah
Forum Newbie
Posts: 11
Joined: Fri May 18, 2012 7:30 am

Re: problem with updating database after dropdownlist submis

Post by mohammedsalah »

finally i found the problem , it tried [syntax]print"$appointment"; [/syntax] in the db_update.php to make sure that i got the right value from the dropdown list
and it was the submitted value + "/" , and discovered the error was in this line of the first code
[syntax]
print"<option value=$appointment/>$appointment";
[/syntax]
so the correction is
[syntax]
print"<option value=$appointment>$appointment";
[/syntax]

just wanted to share that tiny error with u guys ,
thank u so much ur posts really helped me :)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: problem with updating database after dropdownlist submis

Post by Celauran »

mohammedsalah wrote: so the correction is

Code: Select all

print"<option value=$appointment>$appointment";
Actually, it's this

Code: Select all

print"<option value=\"{$appointment}\">{$appointment}";
mohammedsalah
Forum Newbie
Posts: 11
Joined: Fri May 18, 2012 7:30 am

Re: problem with updating database after dropdownlist submis

Post by mohammedsalah »

yes ur correction is great , i was having another error with values with spaces in the middle of it like [monday 2:30 pm] it reads only [monday] and stop before the space, and ur correction solved it
thanks :)
Post Reply