PDA

View Full Version : Resolved not populating table??



liamallan
07-20-2010, 01:26 AM
i know this is pretty basic, so i must be overlooking something! this is my code for a tutorial system, to add a tutorial, but it is not populating the mysql table!

this is my code:

<?php

include("include/session.php");

$hostname = "localhost";
$db_user = "username";
$db_password = "password";
$database = "database";
$db_table = "tutorials";


$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
?>
<body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0">
<table border="1" width="100%" cellspacing="0" id="table1" style="border-width: 0px" cellpadding="0" height="16">
<tr>
<td style="border-style: none; border-width: medium" bgcolor="#3399ff" height="14"><b> <font face="Verdana" size="1" color="#FFFFFF">Submit a tutorial </font></b></td>
</tr>
</table>
<?
if ($session->logged_in)
{
// the user is logged in! We continue...
if (!$_POST['update'])
{

echo("
<form method=\"POST\">
<table width=\"100\">
<tr>
<td align=\"left\" width=\"25%\">
Name of tutorial <br>
</td>
<td align=\"left\">
<input type=\"text\" size=\"25\" class='input' maxlength=\"500\" name=\"name\"></td>
</tr>
<tr>
<td align=\"left\" width=\"25%\">
Description <br>
</td>
<td align=\"left\">
<input type=\"text\" size=\"30\" class='input' maxlength=\"500\" name=\"desc\"></td>
</tr>
<tr>
<td align=\"left\" width=\"25%\">
catagory<br>
</td>
<td align=\"left\">
<select name=\"type\" class='button'>
<option value=\"PHP\">PHP</option>
<option value=\"HTML\">HTML</option>
<option value=\"Photoshop\">Photoshop</option>
<option value=\"CSS\">CSS</option>
<option value=\"Fireworks\">Fireworks</option>
<option value=\"MySQL\">MySQL</option>
<option value=\"Flash\">Flash</option>
<option value=\"Javascript\">Javascript</option>
<option value=\"Tips\">Site tips 'n' tricks</option>
</select></td>
</tr>
<tr>
<td align=\"left\" width=\"25%\">
Tutorial<br></td>
<td align=\"left\">
<textarea name=\"tutorial\" rows='15' class='input' cols='50'>Tutorial here</textarea></td>
</tr>
<td align=\"left\">
<input type=\"submit\" name=\"update\" class='button' value=\"Submit my tutorial!\"></td>
</tr>
</table>
</form>
");
}
else
{

mysql_query("INSERT INTO tutorials(name,desc,type,tutorial,approved,views) values ('".mysql_real_escape_string(stripslashes($_REQUEST['name']))."','".mysql_real_escape_string(stripslashes($_REQUEST['desc']))."','".mysql_real_escape_string(stripslashes($_REQUEST['type']))."','".mysql_real_escape_string(stripslashes($_REQUEST['tutorial']))."',0,0)");

echo("Thank you $session->username, Your tutorial has been posted for admin review. Check back later to see if it has been approved!");

}
}
else
{
// They aren't logged in!
echo ("Sorry, you MUST be logged in to see this page!");
}
?>

any ideas?

fastsol1
07-20-2010, 02:14 AM
What do you mean? Is it not showing the table to input the info or is it not INSERTing the data into the mysql table. I see you're using $_REQUEST and not $_POST when inserting that might be it. I find it usually easier to define the POST variables before INSERT so you don't have to concatenate in the string.

liamallan
07-20-2010, 02:48 AM
i have tried using $_POST, and defining the variables, but still get same error message:

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 'desc,type,tutorial,approved,views) values ('jghjhgj','jghjhgh','PHP','Tutorial h' at line 1

djr33
07-20-2010, 03:03 AM
It's VERY hard to read that code with all of the functions inside the query. It's much easier to read it if you take the time to use $var = mysql_real_escape_string(....; before the query then $var within the query. This will help you see the problem as well.
Please, when posting, make the code as easy to read as possible so that it is easy for us to help you. And that means you'll get more help as well.
In the future for question about MySQL like this, it's best to post the generated query (echo it and cut/paste here), rather than the PHP (or maybe in addition to it) so we know what is generated, since it's hard to tell from the PHP alone.

Regardless, I believe I found the error.

You are missing a space between your table name and the open parenthesis for the list. Just a typo, and it should work.

liamallan
07-20-2010, 03:32 AM
nah, it wasnt the space, still getting same error. the correct values are showing in the error message but i also have an id field in my table which isnt mentioned in the query, do u think that could cause something like this?

p.s. i also echoed the variables, which worked fine

fastsol1
07-20-2010, 03:38 AM
Isn't values in your string supposed to be VALUES

liamallan
07-20-2010, 04:23 AM
its ok, i got it. thanks for ur help guys.

it seems, all i was missing was the id.
here is what i got now:

$id = '';
$name = $_POST['name'];
$tutorial = $_POST['tutorial'];
$approved = '';
$type = $_POST['type'];
$desc = $_POST['desc'];
$views = '';


mysql_query("INSERT INTO tutorials ( `id` , `name` , `tutorial` , `approved` , `type` , `desc` , `views` ) VALUES ('$id', '$name', '$tutorial', '$approved', '$type', '$desc', '$views')");

fastsol1
07-20-2010, 01:43 PM
I find it odd sometimes when that is the solution cause I have never included the $id in my INSERT and it works everytime. Must be some different settings on the server.

djr33
07-20-2010, 07:06 PM
It depends on the configuration of the database: if it is a required and non auto-generating field, then that will generate an error. If not, it is not required. Always, of course, the values and fields listed in the query must match though, but you can omit both for a field that is not required.