Jump to content

Recommended Posts

Hi

 

I found a tutorial on dealing with databases here: http://www.phpfreaks.com/tutorial/php-basic-database-handling

 

It's great, except that I need it to do more than just a name; I need first, last, dob and email. I have it figured out how to order, insert and delete, but can someone help me figure out how to do the updating?

 

The tutorial creates an array called cname and loops through it to do the update, but when I add more fields, it only holds the last fileds (emails in this case)

 

I just need to be able to update all the fields for each user instead of just the name. Sounds easy but I've gotten nowhere

 

Everything I have tried is posted below.

 

Any help? Thanks

 

// INSERT: if we have a name to add...
if($_POST['fname']) {   
// little bit of cleaning...   
$fname = mysql_real_escape_string($_POST['fname']);
$lname = mysql_real_escape_string($_POST['lname']);
$dob = mysql_real_escape_string($_POST['dob']);
$email = mysql_real_escape_string($_POST['email']);
// check for previous entry
$sql_check = "SELECT id FROM test WHERE fname='$fname' AND lname='$lname'";
$res_check = mysql_query($sql_check, $conn) or trigger_error("SQL", E_USER_ERROR);
if (mysql_num_rows($res_check) == 1) {
	while ($row = mysql_fetch_assoc($res_check)) {
		$id = $row['id'];
	}
	// update
	$sql = "UPDATE test SET Current=1 WHERE id='$id'";
	$res = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
} else {
	// insert new name into table   
	$sql = "INSERT INTO test (id, fname, lname, dob, email) VALUES ('', '$fname', '$lname', '$dob', '$email')";   
	$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
}
} // end if
// UPDATE: if we have name(s) to change...
if($_POST['udata']) {   
/*
// for each name to change...   
foreach ($_POST['udata'] as $input => $v) {
	echo "$input = $v<br />";
	$sql = "UPDATE test SET ";
	foreach ($v as $id => $value) {
		echo "$id = $value<br />";
	}
	$sql .= "WHERE ...";
}
echo "<hr />$sql<hr />";
print_r($_POST['udata']);
*/

for ($i = 0; $i < count($_POST['udata']['fname']); $i++) {
	$fname = $_POST['udata']['fname'][$i];
	echo "$i = $fname<br />";
}

/*
foreach($_POST['cname'] as $cid => $cname) {      
	// little bit of cleaning...      
	$id = mysql_real_escape_string($cid);      
	$fname = mysql_real_escape_string($cname);
	$lname = mysql_real_escape_string($cname);
	$dob = mysql_real_escape_string($cname);
	$email = mysql_real_escape_string($cname);
	// update name in the table        
	$sql = "UPDATE test SET fname = '$fname', lname='$lname', dob='$dob', email='$email' WHERE id = '$id'";      
	$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);   
} // end foreach
*/
} // end if         
// DELETE: if we have a name to delete...
if($_GET['id']) {   
// little bit of cleaning...   
$id = mysql_real_escape_string($_GET['id']);   
// delete name from table   
$sql = "UPDATE test SET Current=0 WHERE id= '$id'";   
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
} // end if
// ORDERBY: if one of the links was clicked..
if ($_GET['orderby']) {   
// make an aray of allowed names   
$allowed = array('id','fname', 'lname', 'dob', 'email');   
// bit of cleaning...   
$order = mysql_real_escape_string($_GET['orderby']);   
// is it a valid column name? yes: use it. no: default to 'id'   
$order = (in_array($order, $allowed))? $order : "id";
// if no link clicked, default to 'id'
} else {   
$order = "id";
} // end else
// SELECT: get the list of names from database
$sql = "SELECT id, fname, lname, dob, email FROM test WHERE Current=1 ORDER BY $order";
//$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$result = mysql_query($sql, $conn) or die(mysql_error());
/**** end deal with the database ****/
/**** list everything out ****/
// list columns
echo "<form action = '{$_SERVER['PHP_SELF']}' method = 'post'>\r\n";
echo "<table border = '1'>\r\n";
echo "<tr>\r\n";      
echo "<td><a href = '{$_SERVER['PHP_SELF']}?orderby=id'>id</a></td>\r\n";      
echo "<td><a href = '{$_SERVER['PHP_SELF']}?orderby=fname'>fname</a></td>\r\n";    
echo "<td><a href = '{$_SERVER['PHP_SELF']}?orderby=lname'>lname</a></td>\r\n";   
echo "<td><a href = '{$_SERVER['PHP_SELF']}?orderby=dob'>dob</a></td>\r\n";     
echo "<td><a href = '{$_SERVER['PHP_SELF']}?orderby=email'>email</a></td>\r\n";    
echo "<td>delete</td>\r\n";   
echo "</tr>\r\n";
// loop through list of names 
while ($list = mysql_fetch_assoc($result)) {
echo "<tr>\r\n";      
echo "<td>{$list['id']}</td>\r\n";      
echo "<td><input type = 'text' name = 'udata[fname][{$list['id']}]' value = '{$list['fname']}' />\r\n";
echo "<td><input type = 'text' name = 'udata[lname][{$list['id']}]' value = '{$list['lname']}' />\r\n";
echo "<td><input type = 'text' name = 'udata[dob][{$list['id']}]' value = '{$list['dob']}' />\r\n";
echo "<td><input type = 'text' name = 'udata[email][{$list['id']}]' value = '{$list['email']}' />\r\n";
echo "<td><a href = '{$_SERVER['PHP_SELF']}?id={$list['id']}'>delete</a></td>\r\n";   
echo "</tr>\r\n";
} // end while
// list input box for adding new entry
echo "<tr>";      
echo "<td bgcolor = 'gray'></td>\r\n";      
echo "<td><input type = 'text' name = 'fname' /></td>\r\n";  
echo "<td><input type = 'text' name = 'lname' /></td>\r\n"; 
echo "<td><input type = 'text' name = 'dob' /></td>\r\n"; 
echo "<td><input type = 'text' name = 'email' /></td>\r\n"; 
echo "<td bgcolor = 'gray'></td>\r\n";   
echo "</tr><tr>\r\n";      
echo "<td></td>\r\n";
echo "<td align = 'center'><input type = 'submit' value = 'submit' /></td>\r\n";      
echo "<td></td>\r\n";
echo "</tr></table></form>\r\n";
/**** end list everything out ****/

Link to comment
https://forums.phpfreaks.com/topic/236999-need-help-dealing-with-input-array/
Share on other sites

What you want to do is change this

	echo "<td><input type = 'text' name = 'udata[fname][{$list['id']}]' value = '{$list['fname']}' />\r\n";
echo "<td><input type = 'text' name = 'udata[lname][{$list['id']}]' value = '{$list['lname']}' />\r\n";
echo "<td><input type = 'text' name = 'udata[dob][{$list['id']}]' value = '{$list['dob']}' />\r\n";
echo "<td><input type = 'text' name = 'udata[email][{$list['id']}]' value = '{$list['email']}' />\r\n";

to

	echo "<td><input type = 'text' name = 'udata[{$list['id']}][fname]' value = '{$list['fname']}' />\r\n";
echo "<td><input type = 'text' name = 'udata[{$list['id']}][lname]' value = '{$list['lname']}' />\r\n";
echo "<td><input type = 'text' name = 'udata[{$list['id']}][dob]' value = '{$list['dob']}' />\r\n";
echo "<td><input type = 'text' name = 'udata[{$list['id']}][email]' value = '{$list['email']}' />\r\n";

 

Next change

// UPDATE: if we have name(s) to change...
if($_POST['udata']) {   
/*
// for each name to change...   
foreach ($_POST['udata'] as $input => $v) {
	echo "$input = $v<br />";
	$sql = "UPDATE test SET ";
	foreach ($v as $id => $value) {
		echo "$id = $value<br />";
	}
	$sql .= "WHERE ...";
}
echo "<hr />$sql<hr />";
print_r($_POST['udata']);
*/

for ($i = 0; $i < count($_POST['udata']['fname']); $i++) {
	$fname = $_POST['udata']['fname'][$i];
	echo "$i = $fname<br />";
}

/*
foreach($_POST['cname'] as $cid => $cname) {      
	// little bit of cleaning...      
	$id = mysql_real_escape_string($cid);      
	$fname = mysql_real_escape_string($cname);
	$lname = mysql_real_escape_string($cname);
	$dob = mysql_real_escape_string($cname);
	$email = mysql_real_escape_string($cname);
	// update name in the table        
	$sql = "UPDATE test SET fname = '$fname', lname='$lname', dob='$dob', email='$email' WHERE id = '$id'";      
	$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);   
} // end foreach
*/
} // end if  

to

// UPDATE: if we have name(s) to change...
if($_POST['udata'])
{   
    // update data
    foreach ($_POST['udata'] as $record_id => $fields)
    {
        $sql = "UPDATE test SET ";
        
        $field_data = array();
        foreach($fields as $name => $value)
        {
            $value = mysql_real_escape_string($value);
            $field_data[] = "$field = \"'$value'\"";
        }
        
        $sql .= implode(', ', $field_data);
        $sql .= "WHERE id=$record_id";


        echo "<hr />$sql<hr />";
        $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
    }
} // end if

Thank you!

 

I had to make two minor tweaks, but it was exactly what I needed. I don't know why I didn't think to try the array assignments in another order!

 

Can't thank you enough!  :D

 

Here's what worked:

foreach ($_POST['udata'] as $record_id => $fields)    {        
	$sql = "UPDATE test SET ";                
	$field_data = array();        
	foreach($fields as $name => $value)        {            
		$value = mysql_real_escape_string($value);            
		$field_data[] = "$name = '$value'";        
	}                
	$sql .= implode(', ', $field_data);        
	$sql .= " WHERE id=$record_id";
	$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);    
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.