Jump to content

What's Wrong Here?


HokieTracks

Recommended Posts

I feel like an idiot because I have spent the last hour trying to figure out what is wrong with my code here. I feel like it must be a typo somewhere but I can't find one.

 

<form action="questioninsert.php" method="post" name="question">
Asked By:<br>
<input type="text" name="asker"><br><br>
Answered By:<br>
<input type="text" name="by"><br><br>
Question:<br>
<textarea cols="115" rows="20" name="question"></textarea><br><br>
Answer:<br>
<textarea cols="115" rows="20" name="answer"></textarea>
<input type="submit" value="Post">
</form>

 

<?php

mysql_connect("localhost","*******_*****","********");
mysql_select_db("******_****");	

$asker = mysql_real_escape_string(stripslashes($_POST['asker']));

$by = mysql_real_escape_string(stripslashes($_POST['by']));

$question = mysql_real_escape_string(stripslashes(nl2br(ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $_POST['question']))));

$answer = mysql_real_escape_string(stripslashes(nl2br(ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $_POST['answer']))));

$date = mysql_real_escape_string(date("Y-m-d H:i:s"));

mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')");
?>

Link to comment
Share on other sites

first of all always put a die to your mysql queries

 

mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')")  or die("Cannot execute".mysql_error());

 

secondly I think date is a mysql reserve keyword so you should enclose your field names in ``

Link to comment
Share on other sites

first of all always put a die to your mysql queries

 

mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')")  or die("Cannot execute".mysql_error());

 

secondly I think date is a mysql reserve keyword so you should enclose your field names in ``

 

Oh yeah I totally forgot about die. But enclosing my field names with ' ' didn't help. I've used date as a field name for mysql before without any problems so I don't think it could be a problem with that. I'm getting this error "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 'by, date) VALUES ('sadsafads', 'fasdfsdafdsa', 'dsafdsf', 'dasfd', '2009-11-25 1' at line 1". Which doesn't make a lot of sense to me because if the syntax is right before it then why is it wrong half way through the query.

Link to comment
Share on other sites

He didn't say enclose them in '', he said enclose them with ``. There is a difference.

Oh my bad, but I got it to work without it. I tried changing the field name of "by" to "author" and it started working. Is there some reason that mysql doesn't like "by" as a field name?

Link to comment
Share on other sites

first of all always put a die to your mysql queries

 

mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')")  or die("Cannot execute".mysql_error());

 

Using die() blindly is bad practise. Use

 

<?php
mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')")  or trigger_error("Cannot execute".mysql_error());
?>

 

You should really be checking to see if a query was successful. How is killing the entire script user friendly?

 

secondly I think date is a mysql reserve keyword so you should enclose your field names in ``

Link to comment
Share on other sites

Didn't a previous poster make the statement that "DATE" might be a reserved word? You then find that when changing from "BY" to "AUTHOR" it starts working. So, wouldn't it be logical to assume that "BY" may be a reserved word?

 

A quick google search would have confirmed that: http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

Link to comment
Share on other sites

Didn't a previous poster make the statement that "DATE" might be a reserved word? You then find that when changing from "BY" to "AUTHOR" it starts working. So, wouldn't it be logical to assume that "BY" may be a reserved word?

 

A quick google search would have confirmed that: http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

 

Thanks for the info, no need to get mad though. I'm still learning php so I am just trying to learn as much as I can by asking questions. And, would you really rather have me use google to find an answer than use the website which you seem to be heavily involved in? I would think you would want to be a better php source than google search.

Link to comment
Share on other sites

 

first of all always put a die to your mysql queries

 

mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')")  or die("Cannot execute".mysql_error());

 

 

Using die() blindly is bad practise. Use

 

<?php
mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')")  or trigger_error("Cannot execute".mysql_error());
?>

 

 

You should really be checking to see if a query was successful. How is killing the entire script user friendly?

 

 

 

Link to comment
Share on other sites

Not mad, just trying to get people to think logically and solve thier own problems. That is the essence of programming in my opinion.

 

I have no incentive to people asking questions on this site. I do this for my own benefit. Trying to solve real problems is an activity that helps exercise my mind and it is enlightening to see how others would solve a problem differently than myself. I surely don't want to spend time answering questions that could be answered by a search engine.

Link to comment
Share on other sites

Not mad, just trying to get people to think logically and solve thier own problems. That is the essence of programming in my opinion.

 

I have no incentive to people asking questions on this site. I do this for my own benefit. Trying to solve real problems is an activity that helps exercise my mind and it is enlightening to see how others would solve a problem differently than myself. I surely don't want to spend time answering questions that could be answered by a search engine.

 

Fair enough. My fault for assuming you were trying to be mean.

 

 

first of all always put a die to your mysql queries

 

mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')")  or die("Cannot execute".mysql_error());

 

 

Using die() blindly is bad practise. Use

 

<?php
mysql_query("INSERT INTO questions (question, answer, asker, by, date) VALUES ('$question', '$answer', '$asker', '$by', '$date')")  or trigger_error("Cannot execute".mysql_error());
?>

 

 

You should really be checking to see if a query was successful. How is killing the entire script user friendly?

 

Good point, I never actually thought about that before. Definitely something I will use in the future.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.