MySQL won't update from php form

I’m trying to update my database from my form by using multiple rows. I have two pages, my page with the form and the process page. The process.php page goes through and I don’t see any errors, but my database does not update. I’m missing something, just know sure what. Here’s the code for the form:

[code]




Week



Team Number



Points





Week



Team Number



Points


                    <!--<button type="submit" class="btn btn-default" name="submit">Submit</button>-->
                    <input name="Submit" type="submit" />
				</form>[/code]

Here’s the process.php:
[php]<?php
ini_set(‘display_errors’, ‘On’);
// pdo connection
include(‘includes/database.php’);
// new data
try{

$week = $_POST['week'];
$TeamNumber = $_POST['Team_Number'];
$points = $_POST['Points'];


$sql = 'INSERT INTO week (id,week, Team_Number, Points) VALUES (:id, :week, :Team_Number, :Points)';

// $db_con is pdo connection
$insertTable = $db_con->prepare($sql);

$countArray = count($array);
$i = 0;

while ($i < $countArray) {
   
   $insertTable->bindParam(':week', $_POST['week'], $array[1][$i], PDO::PARAM_STR); 
   $insertTable->bindParam(':Team_Number',$_POST['Team_Number'], $array[2][$i], PDO::PARAM_STR);
   $insertTable->bindParam(':Points',$_POST['Points'], $array[3][$i], PDO::PARAM_STR);
   $insertTable->execute($countArray);
   return $db_con->lastInsertId('id');
   $i++;
}

} catch (PDOException $e) {
echo 'Connection failed: ’ . $e->getMessage();
}
/echo("

\n");
print_r($_POST);
echo("
\n");/

[/php]

Try,

[php]$insertTable->execute();[/php]

You don’t need to pass in a number for the execute statement, just the parameters that need to pass and since you are binding them beforehand, you don’t need to do that.

[php]
$week = $_POST[‘week’];
$TeamNumber = $_POST[‘Team_Number’];
$points = $_POST[‘Points’];
[/php]

This is also unnecessary and likely truncating values on you.

So I have removed both of those things originally, but in another forum, someone told me to add the $_POST’s. With those things removed, it still does not work.

[php]$countArray = count($array);
$i = 0;
while ($i < $countArray) {[/php]

Where does $array come from? If $countArray is < 1 or null then the execute call is never made.

That is a great question. Being a php noob, I’m not exactly sure. I think it comes from when the form is posted. If you uncomment the following lines:
[php]echo("

\n");
print_r($_POST);
echo("
\n");[/php]
it prints out the array based on _POST. I’m thinking that when you put the following value in the input field: “Points[]”, it puts those values in an array. Again I’m a super noob and have just pieced this code together from other tutorials and forums.

$_POST is an array, that doesn’t mean that $array is. Jim just found your biggest problem.

Thanks Jim and thanks for explaining astonecipher. I added the following:
[php]$array = count($_POST);[/php]

but that is giving the value 5. I’m trying to capture 6 fields. I’m also auto incrementing the id and I’m not sure how to tie that in

The whole array counting thing needs to go in the trash. Check this tutorial

https://phpdelusions.net/pdo

I’m even more confused than before

…I think it counts starting with zero: 0,1,2,3,4,5

the database should be what auto increments the id column, not you.

Sponsor our Newsletter | Privacy Policy | Terms of Service