Link to home
Start Free TrialLog in
Avatar of doctorbill
doctorbillFlag for United Kingdom of Great Britain and Northern Ireland

asked on

php loops

I have a form called form3 with the following input boxes:
id1 status1
id2 status2
id3 status3
id4 status4

I need to be able to update records in a mysql database based on SELECTED id values with new status values selected in the status boxes
Each of the boxes is a dropdown feeding off a database of orders and refering to ID of orders and current status

What I am trying to do is to select an id value in each of the boxes and the submit the new status value to the database by using some sort of loop.
I need the php loop to look at the value of id1 and submit the new status1 value of id1 to the database, then the same for id2 / status2 etc

The problem is that I am not so familiar with php loops and would really appreciate some help here
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of doctorbill

ASKER

could you give and example of how would this script be actioned by clicking an a submit button ?
The code above pretty much sums it up. You create a form with the relevant fields, and set it's action to a PHP script.

In the PHP script you loop through the form values and do whatever you need to do with them.

You can often submit the form back to itself. If you do this, check if the POST array is set before you process it.






//form.php
<?php
if ( ! empty($_POST) )
{
  //POST is not empty so it's safe to loop through your fields.
  foreach ($_POST['id'] as $key => $id)
  {
	echo "The ID for Key $key is $id";
	echo "The Status for Key $key is " . $_POST['status'][$key];
  }
}
?>

<form action="form.php" method="POST">
  <input type="text" name="id[1]" />
  <input type="text" name="status[1]" />

  <input type="text" name="id[2]" />
  <input type="text" name="status[2]" />

  <input type="text" name="id[3]" />
  <input type="text" name="status[3]" />

  <input type="text" name="id[4]" />
  <input type="text" name="status[4]" />

  <input type="submit" />
</form>

Open in new window

ok - I understand the form action sends the data to the form.php page.
The form.php page has the following code:
------
//form.php
<?php
if ( ! empty($_POST) )
{
  //POST is not empty so it's safe to loop through your fields.
  foreach ($_POST['id'] as $key => $id)
  {
      echo "The ID for Key $key is $id";
      echo "The Status for Key $key is " . $_POST['status'][$key];
  }
}
?>
------
How is the data sent to the database from the form.php page - I presume I need some code to send the data into the correct table
That's right,

Firstly you'll need to sanitize your fields (to prevent someone from typing something nasty), and then connect to your database and insert your records.

That's a whole different topic to PHP Loops :)



I appreciate the scripts you have shown - they really explain things well.
Could I push you to link the above scripts to a fictitious database connection and table so that I could see how the records would be inserted into the database ?
@doctorbill

That's covering a lot of areas!!

Do you already have your database set up on a server?
What engine are you using (MS SQL, mySQL etc)?
Do you know how to perform basic database queries (Create, Retrieve, Update, Delete - CRUD)?
Have you set up and tested your connection in PHP?
Do you know how to sanitize your fields?

...The list goes on. Coding sites using PHP and mySQL is huge area.

I get the feeling that you're just starting out with PHP and Database, so would suggest that you have a read up on it, at least enough so you understand the basics of what you are trying to achieve.

Have a look at an easy-to-follow tutorial here:- http://www.phpf1.com/tutorial/php-mysql-tutorial.html.

Of course, you've also got the PHP and Database zones here on EE :)

Once you've got the basic understanding and are trying to implement what you've learned, not only will you know enough to ask the right questions to specific problems, but you'll also understand the answers that the Experts offer.

Without a basic understanding, any code I post here will simply generate more questions, all of which are off-topic - PHP Loops.

Good luck with it

Chris








The answer is yes to all the questions. I do a lot of php database work but I have not used loops and arrays, hence the initial question.
I just want to see how the posted information above is put into a database table. I am used to using dreamweaver and mysql insert statements, but I can't see how the posted array data links into the insert statement.
I appreciate all your efforts on this - could you just demonstrate how the information in the array is actually passed into a table ?
OK.

I don't know your table structure, so you'll have to adapt the code to meet your needs. Also not sure whether you're wanting to run UPDATE queries or INSERT queries but the principles are the same.



<?php
foreach ($_POST['id'] as $key => $value)
{
	//Clean and validate your inputs!
	$yourID = mysql_real_escape_string($value);
	$yourStatus = mysql_real_escape_string($_POST['status'][$key]);

	//Create your SQL Statement
        //Are we INSERTing
	$sqlString = "INSERT INTO yourTable (ID, Status) VALUES ('$yourID', '$yourStatus');";

        //or are we UPDATING
	$sqlString = "UPDATE yourTable SET Status = '$yourStatus' WHERE ID = '$yourID';";

	//Run your Query
	$result = mysql_query($sqlString);
	
	//If it failed, then show the reason why
	if (!$result) die('Oh oh! ' . mysql_error());
	
	//See how many rows were affected.
	$numberOfRowsAffected = mysql_affected_rows($result);
}

Open in new window

Thanks so much for your patience and extremely clear descriptions and documentation - the code works fine