Jump to content

[SOLVED] What's wrong with this PHP/MYSQL code?


jb60606

Recommended Posts

A very simple MYSQL table contains four fields: ID, sym, e_price & e_date

 

ID

sym

e_price

e_date

1A1.002007-07-01

2B2.002007-07-02

3C3.002007-07-03

4D4.002007-07-04

 

I have an edit script that populates a simple HTML web form, with those fields, so that the user can edit records in the database.

 

I can populate the form just fine, though editing is another story. Let's say that the contents of the table above are the contents of the database. If I attempt to update record #1 (A, 1.00, 2007-07-01) to reflect a new date (2007-07-15, for example), the record is removed and replaced with a duplicate of the last record in the DB. In other words, record A becomes record D.

 

I think this may have something to do with the checkboxes, but I am not sure. Can you spot anything?

 

Code:


<form name="form1" method="post" action="<?PHP $_SERVER['PHP_SELF'] ?>">
<table width="665" cellpadding="0" cellspacing="0" class="main"><tr bordercolor="#909090" bgcolor="#909090">


<?php
include '../db.php';

$query  = "SELECT DISTINCT sym, id, e_date, e_price FROM tablename ORDER by sym";
$result = mysql_query($query);
$count = mysql_num_rows($result);

while($row = mysql_fetch_array($result)) {  
?>	

<tr>
<td><div align="center"><input name="checkbox[]" id="checkbox[]" type="checkbox" value="<? echo $row['id']; ?>"></div></td>
<td><input type="text" name="new_sym" id="new_sym" value="<? echo $row['sym']; ?>"></td>
<td><input type="text" name="new_e_date" id="new_e_date"  value="<? echo $row['e_date']; ?>"</td>
<td><input type="text" name="new_e_price" id="new_e_price" value="<? echo $row['e_price']; ?>"></td>
<td><A href="notes.php">View/Edit Notes on this Record</A></td>
<td> </td>
<td><input type="submit" name="update" value="Edit Selected"></td>
</tr>

<?PHP
}


if($_POST['update'])
{

$new_sym=$_POST['new_sym'];
$new_e_date=$_POST['new_e_date'];
$new_e_price=$_POST['new_e_price'];

for($i=0;$i<$count;$i++){
$id = $checkbox[$i];
$sql = "UPDATE symbols SET sym='$new_sym', e_date='$new_e_date', e_price='$new_e_price' WHERE id='$id'";
$result = mysql_query($sql);
}

// if successful redirect to records.php

if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=records.php\">";
}
}
mysql_close();
?>

 

Let me know if you need a link to the site.

 

Thanks in advance.

 

Link to comment
Share on other sites

I think you're almost there. However, you'll need to make some other changed to be able to edit more than one row at a time. You'll need all of your form inputs as arrays, and its usually best to make them have a key which is the ID from the database:

<form name="form1" method="post" action="<?PHP $_SERVER['PHP_SELF'] ?>">
<table width="665" cellpadding="0" cellspacing="0" class="main"><tr bordercolor="#909090" bgcolor="#909090">


<?php
include '../db.php';

$query  = "SELECT DISTINCT sym, id, e_date, e_price FROM tablename ORDER by sym";
$result = mysql_query($query);
$count = mysql_num_rows($result);

while($row = mysql_fetch_array($result)) {  
?>	

<tr>
<td><div align="center"><input name="checkbox[<?php echo $row['id'];?>]" id="checkbox[<?php echo $row['id'];?>]" type="checkbox" value="<? echo $row['id']; ?>"></div></td>
<td><input type="text" name="new_sym[<?php echo $row['id'];?>]" id="new_sym" value="<? echo $row['sym']; ?>"></td>
<td><input type="text" name="new_e_date[<?php echo $row['id'];?>]" id="new_e_date"  value="<? echo $row['e_date']; ?>"</td>
<td><input type="text" name="new_e_price[<?php echo $row['id'];?>]" id="new_e_price" value="<? echo $row['e_price']; ?>"></td>
<td><A href="notes.php">View/Edit Notes on this Record</A></td>
<td> </td>
<td><input type="submit" name="update" value="Edit Selected"></td>
</tr>

<?php
}


if($_POST['update'])
{

$new_sym=$_POST['new_sym'];
$new_e_date=$_POST['new_e_date'];
$new_e_price=$_POST['new_e_price'];
$checked = $_POST['checked'];

foreach($checked as $key => $value){
$id= $key;	
$sql = "UPDATE symbols SET sym='$new_sym[$id]', e_date='$new_e_date[$id]', e_price='$new_e_price[$id]' WHERE id='$id'";
$result = mysql_query($sql) or die(mysql_error()."<br />SQL: $sql");

}

// if successful redirect to records.php

if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=records.php\">";
}
}
mysql_close();
?>

 

Ive also changed your loop to a foreach - that way it'll only loop through if the checkbox was selected

Link to comment
Share on other sites

UPDATE:

 

I removed the auto-refresh/redirect at the bottom of the code and found this error:

 

"Warning: Invalid argument supplied for foreach() in (filename) on line 415

 

Is there something in the HTML that I am supposed to pass the script? Where does $checked get it's values from? Is it something built into PHP?

Link to comment
Share on other sites

 

foreach($checked as $key => $value){ are refering to this??

 

then i think that was wrong

foreach($checked as $key ){ remove the $value because it the array from post right

and that is not multidimensional

 

Link to comment
Share on other sites

Thanks for your help.

 

Unfortunately I'm still getting the same error.

 

If it's any help to anyone, I had PHP print out everything that was posted to the script and I didn't see anything regarding the status of the checkbox anywhere. I'm very new to PHP/MYSQL, but doesn't it need to be in there for us to use it as a variable?

 

Thanks again.

 

Array ( [update] => Update Selected [checkbox] => Array ( [184] => 184 ) [sym] => Array ( [184] => AAPL [223] => ACM [183] => AKAM [191] => AM [224] => AMD [213] => BIDU [210] => CELG [197] => CLWR [198] => CMG [220] => CRM [205] => CROX [221] => CSUN [203] => DNA [209] => DNDN [190] => EBAY [212] => ENMD [196] => EPCT [215] => FCX [207] => GE [219] => GME [189] => GOOG [182] => GRMN [195] => HANS [206] => HGSI [217] => INFY [201] => INFY [208] => IVAN [202] => LLNW [188] => MNST [192] => MS [216] => OIIM [187] => OMTR [194] => OPLK [214] => OXPS [185] => SNCR [211] => TM [218] => VCLK [193] => VCLK [199] => VLO [186] => WU [222] => XING [200] => ZOLT [204] => ZOLT ) [e_date] => Array ( [184] => 0000-00-00 [223] => 2007-05-16 [183] => 2007-06-28 [191] => 2007-06-21 [224] => 2007-05-15 [213] => 2007-05-31 [210] => 2007-06-04 [197] => 2007-06-14 [198] => 2007-06-14 [220] => 2007-05-21 [205] => 2007-06-07 [221] => 2007-05-17 [203] => 2007-06-08 [209] => 2007-06-05 [190] => 2007-06-22 [212] => 2007-06-01 [196] => 2007-06-15 [215] => 2007-05-30 [207] => 2007-06-06 [219] => 2007-05-22 [189] => 2007-06-22 [182] => 2007-06-29 [195] => 2007-07-15 [206] => 2007-06-07 [217] => 2007-05-24 [201] => 2007-06-12 [208] => 2007-06-06 [202] => 2007-06-11 [188] => 2007-06-25 [192] => 2007-06-20 [216] => 2007-05-30 [187] => 2007-06-25 [194] => 2007-06-18 [214] => 2007-05-31 [185] => 2007-06-27 [211] => 2007-06-01 [218] => 2007-05-22 [193] => 2007-06-19 [199] => 2007-06-13 [186] => 2007-06-26 [222] => 2007-05-16 [200] => 2007-06-13 [204] => 2007-06-08 ) [e_price] => Array ( [184] => 6 [223] => 23 [183] => 48 [191] => 26 [224] => 15 [213] => 107 [210] => 63 [197] => 22 [198] => 83 [220] => 46 [205] => 88 [221] => 13 [203] => 76 [209] => 9 [190] => 32 [212] => 2 [196] => 3 [215] => 77 [207] => 37 [219] => 38 [189] => 127 [182] => 74 [195] => 42 [206] => 11 [217] => 50 [201] => 52 [208] => 2 [202] => 23 [188] => 42 [192] => 89 [216] => 10 [187] => 22 [194] => 15 [214] => 25 [185] => 26 [211] => 123 [218] => 35 [193] => 30 [199] => 73 [186] => 21 [222] => 15 [200] => 37 [204] => 35 ) )

 

Link to comment
Share on other sites

;)I had PHP print out everything that was posted to the script and I didn't see anything regarding the status of the checkbox anywhere

 

ok can you tell us what do you realy mean

 

well, the variable "$checked" is declared in the PHP code:

 

$checked = $_POST['checked'];

 

And is then used in the foreach loop:

 

foreach($checked as $key){
             $id=$key;

 

Though I don't see anything about the state of the checkbox (and by "state" I mean checked or unchecked) being posted from the form to the PHP code. As a matter of fact, I thought that only checked check boxes were posted in PHP and the unchecked were ignored. That's my guess as to why PHP is saying that the foreach statement is invalid, but, again, I'm rather new to all of this.

 

Thanks again.

 

 

 

Link to comment
Share on other sites

I just figured out the problem, and I'm almost too embarrassed to say what it was.

 

In the HTML, "checkbox[]" is posted to the script. In the PHP code, it was looking for $_POST['checked'];. I thought they were two different things before but it hit me when I was writing out that reply to you.

 

Thank you GingerRobot and Teng, for all your help. I don't know what I'd do without this website.

Link to comment
Share on other sites

Whoops, that was partly my fault. Glad you sorted it out

 

not a problem. Without your code to point me in the right direction, I probably would have given up and gone another route, probably altering it so that the user would have to tediously update each record one at a time.

 

Thanks again.

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.