Jump to content

mysql update and fetch not working as planned


Russia

Recommended Posts

Hello, I have this simple php form that updates the table, and also shows what the information in it is.

 

Here it is:

 

 

<strong>Update multiple rows in mysql</strong><br>

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

// Count table rows
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">


<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Month</strong> <strong>Date</strong> </td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td align="center"><input name="month[]" MAXLENGTH="3" size="3" type="text" id="month" value="<?php echo $rows['month']; ?>">-
                   <input name="date[]" MAXLENGTH="2" size="2" type="text" id="date" value="<?php echo $rows['date']; ?>">
</td>
<td align="center"><input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>"></td>
<td align="center"><input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this

if (isset($_POST['Submit'])) { 

for($i=0;$i<$count;$i++){

$month = $_POST['month'];
$date = $_POST['date'];

$lastname = $_POST['lastname'];
$email = $_POST['email'];

$name = $month."<br>".$date;



$sql1="UPDATE $tbl_name SET name='$name[$i]', month='$month[$i]', date='$date[$i]', lastname='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
}

if($result1){
header("location:update2.php");
}
?>    




<?php
$result = mysql_query("SELECT * FROM test_mysql")
or die(mysql_error());  

echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['name'];
    echo "</td><td>"; 
    echo $row['lastname'];
    echo "</td></tr>"; 
} 

echo "</table>";
?>

 

 

What it does is this, it gets the row from the database, echos it and I am able to edit it and then save.

 

The thing is, it works with the updating but im trying to combine 2 variables into 1 and update that one also.

 

Im trying to combine month and date textboxes into one variable in the database that is name.

 

It for some reason updates the column name to A instead of what I had in the 2 fields month and date.

 

Here is where I combine both fields into one.

$name = $month."<br>".$date;

 

And here is where $month and $date is set.

$month = $_POST['month'];
$date = $_POST['date'];

 

 

Here is a picture.

 

flftie.jpg

 

dpbp6h.jpg

Link to comment
Share on other sites

In this case $i stands for the index value of the array you submitted from your forms

so on the first loop $i=0 which is the first value in the array

second loop $i=1 which is the second value in the array and so on until $i is greater than the number counted in the array

Link to comment
Share on other sites

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.