FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


A PHP problem !





abhinav_shah
The following is a php script which runs perfectly fine. But I have one problem.
=> When I assign a variable the value from the form eg.
$acc = $_POST["accno"] it doesn't work. But if I give
$acc = $_POST[accno] it works (accno without quotes).
Also i the query
$result = mysql_db_query("book_info","insert into info values('$acc','$title','$auth','$edition','$pub')"); should I give single quotes to the variable names i.e $acc, $title ...?
Please help me out , when I should give single quotes or double quotes or no quotes at all ....?


Quote:
<HTML>
<HEAD>
<TITLE> Program 8.php </TITLE>

</HEAD>
<BODY>
<form action = "" method = "post">
Accession number <input type ="text" name = "accno"><p>
Title <input type = "text" name = "title"><p>
Authors <input type = "text" name = "authors"><p>
Edition <input type = "text" name = "edition"><p>
Publication <input type = "text" name = "pub"><p>
<input type = "submit" value = "send">
<input type = "reset"><p><hr>
</form>

<?
$mysql = mysql_connect("localhost","root","msrit");
$acc = $_POST[accno];
$title = $_POST[title];
$auth = $_POST[authors];
$edition = $_POST[edition];
$pub = $_POST[pub];
$result = mysql_db_query("book_info","insert into info values('$acc','$title','$auth','$edition','$pub')");
$r1 = mysql_db_query("book_info","select *from info");
while ( $array = mysql_fetch_row($r1) ) {
foreach($array as $f) {
print "$f ::";
}
print "<hr>";
}
?>
</BODY>
</HTML>
SamiTheBerber
abhinav_shah wrote:
The following is a php script which runs perfectly fine. But I have one problem.
=> When I assign a variable the value from the form eg.
$acc = $_POST["accno"] it doesn't work. But if I give
$acc = $_POST[accno] it works (accno without quotes).
Also i the query
$result = mysql_db_query("book_info","insert into info values('$acc','$title','$auth','$edition','$pub')"); should I give single quotes to the variable names i.e $acc, $title ...?
Please help me out , when I should give single quotes or double quotes or no quotes at all ....?

I fixed the code and now it should work. Inside [] should use single quotes. In code like echo should use double quotes. Do not ever use CAPS in TAGS!

form.php:
Code:
<?
if ($action == "post") {
   $mysql = mysql_connect("localhost","root","msrit");
   $acc = $_POST['accno'];
   $title = $_POST['title'];
   $auth = $_POST['authors'];
   $edition = $_POST['edition'];
   $pub = $_POST['pub'];
   $result = mysql_db_query("book_info","insert into info values('$acc','$title','$auth','$edition','$pub')");
   $r1 = mysql_db_query("book_info","select *from info");
   while ( $array = mysql_fetch_row($r1) ) {
      foreach($array as $f) {
         print "$f ::";
      }
      print "<hr>";
   }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Program 8.php </title>
</head>
<body>
<form action = "form.php?action=post" method = "post">
<p>Accession number <input type ="text" name = "accno"></p>
<p>Title            <input type = "text" name = "title"></p>
<p>Authors          <input type = "text" name = "authors"></p>
<p>Edition          <input type = "text" name = "edition"></p>
<p>Publication      <input type = "text" name = "pub"></p>
<p><input type = "submit" value = "send">
<input type = "reset"></p><hr>
</form>
</body>
</html>
abhinav_shah
I tried the modified script which you have posted. But it's not working for me.
I saw the error in Apache Logs, and the error says : Undefined variable: action
Help me out...
mathiaus
From quickly browsing your code the php seesm fine. The variables are not being sent because of your form though!

Code:
<form action="" method="post">
Accession number <input type="text" name="accno"><p>
Title <input type="text" name="title"><p>
Authors <input type="text" name="authors"><p>
Edition <input type="text" name="edition"><p>
Publication <input type="text" name="pub"><p>
<input type="submit" value="send">
<input type="reset"><p><hr>
</form>

Way to many spaces here Wink


One minor thing in the php. When you set the variable $r1 you should add a space after the *
ammonkc
Quote:
Do not ever use CAPS in TAGS!


I'm just curious, why you say not to ever use caps in tags. I've never heard that before. is there any reason in particular? or is it just convention to use lower case?

and one more thing, why is it better to use double quotes for echo's? in my code, I always use single quotes inside of []. but I usually only use double quotes in my echo's if I have single quotes inside of the quote. if I don't have to use quotes inside of quotes, I just use single quotes. is that bad convention?
mathiaus
Caps in tags arent XHTML compient. Other than that I cant say I know of any other reason why. They should display the same.

about the quotes, basiclly from what I understand and has always worked for me is that variables dont parse when under single quotes but do under double quotes. Slightly more detail below
http://www.trans4mind.com/personal_development/phpTutorial/quotes.htm
ammonkc
yeah, I normally always use lowercase tags anyway. but sometimes my editing program will automatically insert caps, and i've never seen it render differently.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.