...

View Full Version : Submiting data into a mysql table from a form



Andy92
08-28-2006, 07:18 PM
Hi there,

I have 2 files called add.php and create.php

In create.php i have...



<form action="/admin/news/add.php" method="post" name="news" id="news">
<table width="100%">
<tr>
<td><div><div class="text_bold">Id: <span class="text_link_colour">*</span></div></div></td>
<td width="83%"><input name="id" type="text" id="id" style="width: 475px;" maxlength="11" /></td>
</tr>
<tr>
<td><div><div class="text_bold">Title: <span class="text_link_colour">*</span></div></div></td>
<td width="83%"><input name="title" type="text" id="title" style="width: 475px;" maxlength="100" /></td>
</tr>
<tr>
<td><div><div class="text_bold">Mini: <span class="text_link_colour">*</span></div></div></td>
<td><input name="mini" type="text" id="mini" style="width: 475px;" maxlength="200" /></td>
</tr>
<tr>
<td><div><div class="text_bold">Full: <span class="text_link_colour">*</span></div></div></td>
<td rowspan="2"><textarea name="full" rows="10" id="full" style="width: 475px;"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td><div><div class="text_bold">Image: <span class="text_link_colour">*</span></div></div></td>
<td><input name="image" type="text" id="image" style="width: 475px;" value="http://www.allsortshop.com/images/news/image.jpg" /></td>
</tr>
<tr>
<td><div><div class="text_bold">Posted: <span class="text_link_colour">*</span></div></div></td>
<td><input name="posted" type="text" id="posted" style="width: 475px;" value="00/00/0000" maxlength="50" /></td>
</tr>
<tr>
<td><div><div class="text_bold">Full Story: <span class="text_link_colour">*</span></div></div></td>
<td><input name="full_story" type="text" id="full_story" style="width: 475px;" value="The url of the news story" maxlength="500" /></td>
</tr>
<tr>
<td><div>
<div class="text_bold">Name: <span class="text_link_colour">*</span></div>
</div></td>
<td><input name="news_name" type="text" id="news_name" style="width: 475px;" value="The name of the news company" maxlength="300" /></td>
</tr>
<tr>
<td><div><div class="text_bold">News Url: <span class="text_link_colour">*</span></div></div></td>
<td><input name="news_url" type="text" id="news_url" style="width: 475px;" value="The news companys url address" maxlength="200" /></td>
</tr>
<tr>
<td><div><div class="text_bold">Posted By: <span class="text_link_colour">*</span></div></div></td>
<td><input name="user" type="text" id="user" style="width: 475px;" maxlength="200" /></td>
</tr>
<tr>
<td><div><div class="text_bold">Day: <span class="text_link_colour">*</span></div></div></td>
<td><input name="day" type="text" id="day" style="width: 475px;" maxlength="3" /></td>
</tr>
<tr>
<td height="17" colspan="2"><span class="text_link_colour">*</span> = Required Field </td>
</tr>
<tr>
<td height="23" colspan="2"><div align="center">
<input name="Send" type="submit" id="Send" value="Send!" />
</div></td>
</tr>
</table>
</form>


Then in add.php i have the following php code...



<?php
$server = "localhost"; // server to connect to.
$database = "my database"; // the name of the database.
$db_user = "database user"; // mysql username to access the database with.
$db_pass = "my password"; // mysql password to access the database with.
$table = "news"; // the table that this script will set up and use.
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
// insert the data
$insert = mysql_query("insert into $table values ('".$_POST['id']."','".$_POST['title']."',
'".$_POST['mini']."','".$_POST['image']."','".$_POST['full_story']."','".$_POST['news_name']."','".$_POST['news_url']."','".$_POST['user']."','".$_POST['day']."','NULL','".$_POST['full']."','".$_POST['posted']."')")
or die("Could not insert data because ".mysql_error());
// print a success message
echo "Your news story has been added to the allsortshop news database! Have a look on the site to see your news post!";
?>


Then when i fill in the form and press submitt, it goes to add.php and displays this error...

Could not insert data because Column count doesn't match value count at row 1

How can i fix this so that it will add it to the mysql database???

:confused:

guelphdad
08-28-2006, 07:34 PM
You are using either more or less columns than your table contains on your insert statement. Also don't use '' around NULL or it will store the string NULL which isn't the same thing as NULL.

Andy92
08-28-2006, 08:20 PM
Ok, ive got it all working now.

Thanks! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum