is it possible? I have to bring up the author and joke, written by visitors to my website directly in php page!!!
I have two tables and a report
// If a joke has been submitted, or authorname
// add it to the database.
if (isset($_POST[‘joketext’]), isset($_POST[‘name’]) {
$joketext = $_POST[‘joketext’]; $name = $_POST[‘name’];
$sql = "INSERT INTO joke, author SET
joketext=‘$joketext’,
jokedate=CURDATE(), name=$name WHERE authorid=author.id";
if (@mysql_query($sql)) {
echo ‘<p>Your joke has been added.</p>’;
} else {
echo '<p>Error adding submitted joke: ’ .
mysql_error() . ‘</p>’;
It is possible, but you will probably want to learn the basics of SQL syntax as I have already identified an error in the SQL string(the SET keyword is used with update clause, not insert clause). Also I guess it wont hurt to repeat again that you need to get rid of using mysql_ functions, instead revise your code with MySQLi or PDO.
The syntax of INSERT INTO … SET is actually valid - it’s just more conventional to use INSERT INTO … VALUES. Check out the MySQL manual (first paragraph) here: INSERT Syntax
Are you asking us whether it is possible to display the user input from the database, or where you’re going wrong on your INSERT statement, or both? Your post isn’t too clear on that - a clarification on your issue(s) and perhaps a post of your two tables would help.
One MySQL syntax error I can see already is that you’re not encasing the $name variable within single quotes within your query (assuming it will have a string value). You’re also wide open to injection attacks - escaping data within the application before sending it to the database (whether for querying or storing) should always be done.
You will need to perform a JOIN upon the two tables, where the author.id references the joke.authorid. Here’s how I’d go about doing your SQL query:
SELECT joke.joketext, author.name FROM joke INNER JOIN author ON joke.authorid = author.id;
That will grab all jokes and the corresponding author name from your joke table. If you’d like to limit the result set of jokes to just one author, then you can use the WHERE clause to narrow down your search (e.g. WHERE author.id = 1).
good morning I have attached the full script !!!
the yellow part is to be changed with
SELECT joketext, name From joke, authored WHERE author = author.it
but I know that I must to change other parts to refer to “two tables” and no more than “one table”!
if you can indicate the parts to be changed in “the code”?
-I guess I should add that in the initial form textarea for ‘name’ etc etc.