Reputation: 75
i wanted the code to insert the same value to different rows that are checked in the list(the list is queried from the database.) but it says
"Notice: Array to string conversion in \enrol.php on line 5",
here is my section.php
<table border=1px>
<th>SELECT</th>
<th>ID NUMBER</th>
<th>NAME</th>
<th>COURSE</th>
<th>YEAR</th>
<?php session_start();
include('connect.php');
$group=$_REQUEST['idEdit'];
echo $group;
$sql="SELECT * FROM login WHERE section IS NULL or section = 0";
$result=mysql_query($sql) or die(mysql_error() );
while($arr=mysql_fetch_array($result))
{
echo '<tr><td><form action="enrol.php" method="post"><input type="checkbox" name="idnumber[]" value="'.$arr['idnumber'].'"></td>'.
'<td>'.$arr['idnumber'].", </td>".
'<td>'.$arr['lastname'].",".$arr['firstname']." </td>".
'<td>'.$arr['course']." </td>".
'<td>'.$arr['year']." </td></tr>";}
$idnumber=$arr['idnumber'];
?>
<input type="hidden" value="<?php $group ?>" name="group">
<input type="submit" value="Enrol Student" name="submit">
</form>
</table>
and here is my enrol.php
<?php
if(isset($_REQUEST['submit'])){
$ids = join (', ', $_POST['idnumber']);
$sql="INSERT INTO login (section) values ('$_POST[group]') WHERE idnumber='$ids'";
mysql_query($sql);
$count=mysql_affected_rows();
if($count==1){
echo '<script type="text/javascript">alert("Added Sucessfully")</script>';
}else{
echo '<script type="text/javascript">alert("Error!")</script>';
}
}
?>
how can i solve this problem? i have been trying to search for a solution but i cant seem to find any. please help.
Upvotes: 0
Views: 195
Reputation: 75
i got the answer thanks to @thumber nirmal and all those who responded with this question. here is the working code for enrol.php
<?php include('connect.php');
if(isset($_REQUEST['submit'])){
$ids = implode(', ', $_POST['idnumber']);
$sql="update login set section='".$_POST['group']."' WHERE idnumber IN ($ids)";
echo $sql;
$query = mysql_query($sql) or die(mysql_error());
mysql_query($sql);
$count=mysql_affected_rows();
if($count==1){
echo '<script type="text/javascript">alert("Added Sucessfully")</script>';
}else{
echo '<script type="text/javascript">alert("Error!")</script>';
}
}
?>
Upvotes: 0
Reputation: 471
I think,You are getting the value of the hidden field group null. First of all You echo group value in the hidden Field.
<input type="hidden" value="<?php echo $group; ?>" name="group">
I hope your problem will be Solved. You got my point?
Upvotes: 1
Reputation: 546
There is some quote issue when you are fetching post values. Try it.
$sql="INSERT INTO login (section) values ('".$_POST["group"]."') WHERE idnumber='".$ids."'";
Upvotes: 0
Reputation: 13785
Use implode
<?php
if(isset($_REQUEST['submit'])){
$ids = implode(', ', $_POST['idnumber']);
$sql="INSERT INTO login (section) values ('$_POST[group]') WHERE idnumber='$ids'";
mysql_query($sql);
$count=mysql_affected_rows();
if($count==1){
echo '<script type="text/javascript">alert("Added Sucessfully")</script>';
}else{
echo '<script type="text/javascript">alert("Error!")</script>';
}
}
?>
Upvotes: 0
Reputation: 1617
use implode instand of join
$ids = implode(', ', $_POST['idnumber']);
Upvotes: 0