Page 1 of 1

Script works and then it doesn't?

Posted: Sat Apr 18, 2009 9:54 am
by nicademus
Hello everyone!
I am using the same script to input data into my MySQL database with php. For some reason on one of the tables the code repeats the last entry but I cannot figure out why. It works fine for a different table with no repeats. Any help would be greatly appreciated :)

HTML

Code: Select all

 
<form action="db-collections-script.php" method="post">
<div id="header">
<img src="admin_img/header.png" />
</div><!-- end header     -->
<div id="container">
<legend>Collections - Designers and the categories found on retailers sites</legend>
<ul id="list">
  <li>Company Name: <input type="text" name="co_name[]" /></li>
  <li>Link: <input type="text" name="link[]" /></li>
  <li>Collection Name: <input type="text" name="text[]" /></li><br /><br />
  <li>Company Name: <input type="text" name="co_name[]" /></li>
  <li>Link: <input type="text" name="link[]" /></li>
  <li>Collection Name: <input type="text" name="text[]" /></li><br /><br />
  <li>Company Name: <input type="text" name="co_name[]" /></li>
  <li>Link: <input type="text" name="link[]" /></li>
  <li>Collection Name: <input type="text" name="text[]" /></li><br /><br />
  <li>Company Name: <input type="text" name="co_name[]" /></li>
  <li>Link: <input type="text" name="link[]" /></li>
  <li>Collection Name: <input type="text" name="text[]" /></li><br /><br />
  <li>Company Name: <input type="text" name="co_name[]" /></li>
  <li>Link: <input type="text" name="link[]" /></li>
  <li>Collection Name: <input type="text" name="text[]" /></li><br /><br />
  <li>Company Name: <input type="text" name="co_name[]" /></li>
  <li>Link: <input type="text" name="link[]" /></li>
  <li>Collection Name: <input type="text" name="text[]" /></li><br /><br />
  <li>Company Name: <input type="text" name="co_name[]" /></li>
  <li>Link: <input type="text" name="link[]" /></li>
  <li>Collection Name: <input type="text" name="text[]" /></li><br /><br />
  <li>Company Name: <input type="text" name="co_name[]" /></li>
  <li>Link: <input type="text" name="link[]" /></li>
  <li>Collection Name: <input type="text" name="text[]" /></li><br /><br />
  <li>Company Name: <input type="text" name="co_name[]" /></li>
  <li>Link: <input type="text" name="link[]" /></li>
  <li>Collection Name: <input type="text" name="text[]" /></li><br /><br />
  <li>Company Name: <input type="text" name="co_name[]" /></li>
  <li>Link: <input type="text" name="link[]" /></li>
  <li>Collection Name: <input type="text" name="text[]" /></li><br /><br />
</ul><br />
 
<input type="submit" value="Submit To Database?" />
</form>
 
PHP

Code: Select all

 
$size_array = count($_POST['co_name']);//count the nr of times this field occurs in the array, so we can use this to process with a loop
//now INSERT
for ($i=0; $i<$size_array; $i++){
$query = 'INSERT into collections'.
" values ( '".mysql_real_escape_string($_POST['co_name'][$i])."', '".mysql_real_escape_string($_POST['link'][$i])."', '".mysql_real_escape_string($_POST['text'][$i])."')";
$result = mysql_query($query) or die ("Error in query: $query");
} 
if (!mysql_query($query,$con))
  {
  die('Error: ' . mysql_error());
  }
 
Not sure where to look for the solution or the problem as I cannot see any differences in the code - perhaps I missed something?

Thanx!

Re: Script works and then it doesn't?

Posted: Sat Apr 18, 2009 10:38 am
by requinix

Code: Select all

$result = mysql_query($query) or die ("Error in query: $query");
} 
if (!mysql_query($query,$con))
Oops.