0

hello

this my code :

<?php

$con=mysql_connect("localhost","root","");

if(!$con)
{
die('can not connect'.mysql_error());
}
mysql_select_db("test", $con );
$others="others";
$sql="SELECT * FROM $others";
$result=mysql_query($sql);
$num = mysql_num_rows($result);

echo"<thead>
<tr> <th>#</th>   <th> id </th>   <th>topic</th></thead>";
    echo"<tbody>";

$i=0;

echo"

<

table border='1' width='100%' id='table1'>";
echo"<thead>
<tr> <th>#</th> <th> id </th> <th>topic</th></thead>";
echo"<tbody>";
$i=0;

while( $i < $num ) {

$id = mysql_result( $result , $i , "id" );
$topic = mysql_result( $result , $i , "topic" );

echo"<tr>";
?>

<?php echo"$id "; echo"$topic ";

echo""; ++$i; } echo""; echo""; ?>

<?php mysql_close($con); ?>

-------
i want if user selected checkbox then choose save ,, insert information(id and topic from "other table" to other_2 table"

please help me ,,
how can do that :(

Edited by don't give up: Modify the code

2
Contributors
16
Replies
17
Views
5 Years
Discussion Span
Last Post by don't give up
Featured Replies
  • 2

    Basicaly what you do you add a ceckbox to each row in the table, wrap the table in `<form></form>` tags, add hidden fields which hold the topics and add a submit button. Then you first check if the form has been submitted. If yes you process it (do the insert) … Read More

2

Basicaly what you do you add a ceckbox to each row in the table, wrap the table in <form></form> tags, add hidden fields which hold the topics and add a submit button. Then you first check if the form has been submitted. If yes you process it (do the insert) and then as usually draw the table. See comments in the code (the code was not tested, but you will get the idea).

<?php

// do the connection stuff
$con=mysql_connect("localhost","root","");
if(!$con) {

    die('can not connect'.mysql_error());
}
mysql_select_db("test", $con );

// check if the form was already submitted, and process it, if it was
if(isset($_POST['submit'])) {

    // check if any of the checkboxes have been selected
    // (selected checkboxes will be returned in a $_POST['cbsave'] array,
    // if none was selected, $_POST['cbsave'] will not be set)
    if(isset($_POST['cbsave']) and !empty($_POST['cbsave'])) {

        foreach($_POST['cbsave'] as $id) {

            // check if an appropriate hidden field with the topic exists and
            // is not empty
            $topic_key = "topic_$id";
            if(isset($_POST[$topic_key]) and !empty($_POST[$topic_key])) {

                $new_topic = $_POST[$topic_key];

                // insert into others_2 table
                $sql = "INSERT INTO others_2 VALUES ($id, '$new_topic')";
            }
        }
    }
}

// select from others to fill the table
$others="others";
$sql="SELECT * FROM $others";
$result=mysql_query($sql);
$num = mysql_num_rows($result);

// commented this one out since it appears lower in the script!
// echo"<thead>
// <tr> <th>#</th> <th> id </th> <th>topic</th></thead>";
// echo"<tbody>";

// put whole thing in a form that has action set to itself (or other page if you wish)
echo '<form action="#" method="post">';

// fixed html code for header row, added one more column for checkbox
echo "<table border='1' width='100%' id='table1'>";
echo "<thead><tr><th>#</th><th>id</th><th>topic</th><th>Save to other_2</th>/tr></thead>";

echo"<tbody>";

$i=0;

while( $i < $num ) {

    $id = mysql_result( $result , $i , "id" );
    $topic = mysql_result( $result , $i , "topic" );

    // save topic in a hidden field so you will be able to read it when form is posted
    echo "<input type=\"hidden\" name=\"topic_$id\" value=\"$topic\" />";

    // html code for the curent checkbox (with a value for current id)
    $cb_save = '<input type="checkbox" name="cbsave[]" value="' . $id . '"/>';

    // echo one row (#, id, topic and the checkbox)
    echo"<tr><td>$i</td><td>$id</td><td>$topic</td><td>$cb_save</td></tr>";

    ++$i;
}

// end the table
echo"</tbody>";
echo"</table>";

// add a submit button
echo '<input type="submit" name="submit" value="Save to other_2" />';

// end the form
echo '</form>';

mysql_close($con);
?>

Edited by broj1: Typos

Votes + Comments
thank you
0

sory but i'm not understanding,how can do for my code ? ,this is my code :

    <?php
$con=mysql_connect("localhost","root","");  

  if(!$con)  
  {  
   die('can not connect'.mysql_error());  
  }  
   mysql_select_db("scms", $con );    
   $others="others";
   $sql="SELECT * FROM $others";
    $result=mysql_query($sql);
    $num = mysql_num_rows($result);
    $row = mysql_fetch_array($result);

   echo"<table border='1' width='100%' id='table1'>";  
    echo"<thead>
    <tr> <th>#</th>    <th>topic</th><th> author </th>   <th>date</th> 
        </thead>";
        echo"<tbody>";
   $i=0;  



  while( $i < $num )  { 

   $id  = mysql_result( $result , $i , "id"    );    
   $topic = mysql_result( $result , $i , "topic" );  
   $author = mysql_result( $result , $i , "author" );  
   $content = mysql_result( $result , $i , "content" );  
    $picture = mysql_result( $result , $i , "picture" );  
    $date=mysql_result( $result , $i , "date" ); 
   echo"<tr>";
  ?> 
  <form name="form1" method="post" action="">
    <td> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id']?>"> </td> 
<?php

   echo"<td width='20%'>$topic </td>";  
   echo"<td width='20%'>$author </td>"; 
     echo"<td width='20%'> $date </td>";

   echo"</tr>"; 

   ++$i;  
  }  
 echo"</tbody>";
  echo"</table>";  
  ?>

  <input name="save" type="submit" id="save" value="save">
  </form>
  <?php


mysql_close($con);  
?>
0

Sorry I thought you have only the topic. In the case above the solution will be similar. First check if the form was submited and if yes, get the array of IDs. Then just insert into others_2 table by selecting from others. The code would be something like

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

    if(isset($_POST['checkbox']) and !empty($_POST['checkbox'])) {

        foreach($_POST['checkbox'] as $id) {

            $sql = "INSERT INTO other_2 SELECT FROM other WHERE id=$id";
        }
    }
}

But be careful. Tables orther and other_2 have to have exactly the same structure otherwise you might not get what you expect. You can also make the query more specific by specifying the fields. And also it would be a good idea to check for existing records in other_2 table not to insert duplicate rows.

0

thanks alot :)

It works :)
However, this problem prints " Error: Duplicate entry '1' for key 'PRIMARY' "

but id is diffrent !

0

Post your table structure. Which fiekd is the primary key? Do you have an autoincrement field in the table? You should probably check wheter the id already exists in other_2 table before inserting it. to avoid duplicate records.

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

    if(isset($_POST['checkbox']) and !empty($_POST['checkbox'])) {

        foreach($_POST['checkbox'] as $id) {

            // check if the record with this id exists
            $sql = "SELECT COUNT(*) FROM other_2 WHERE id=$id";

            $result=mysql_query($sql);

            // insert only if record does not exist
            if(mysql_num_rows($result) == 0) {

                $sql = "INSERT INTO other_2 SELECT FROM other WHERE id=$id";
            }                
        }
    }
}
0

maybe error here ??
<td> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id']?>"> </td>

??
becuase just add row id=1

0

Have a look at the html source (i.e in Firefox( right click on the page and select View Page Source. In the source you can check whether the checkboxes have proper value attributes (each checkbox should have different id for value attribute). Also check if there are any errors in html (usually colored red).

Edited by broj1: typo

0

Known error, but I did not know how to fix

Look at the value !!!
this source code :

<table border='1' width='100%' id='table1'><thead>
    <tr> <th>#</th>    <th>topic</th><th> author </th>   <th>date</th> 
        </thead><tbody><tr> 
  <form name="form1" method="post" action="">
    <td> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="1"> </td> 
<td width='20%'>LSD 'helps alcoholics to give up drinking' </td><td width='20%'>BBC HEALTH </td><td width='20%'> 2012-03-10 11:18:39 </td></tr><tr> 
  <form name="form1" method="post" action="">
    <td> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="1"> </td> 
<td width='20%'>Swiss firm Hublot unveils $5m diamond watch </td><td width='20%'>BBC OTHERS </td><td width='20%'> 2012-03-10 21:10:26 </td></tr><tr> 
  <form name="form1" method="post" action="">
    <td> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="1"> </td> 
<td width='20%'>Oreo cookie centenary marked in US and other countries </td><td width='20%'>BBC OTHERS </td><td width='20%'> 2012-03-10 21:15:46 </td></tr><tr> 
  <form name="form1" method="post" action="">
    <td> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="1"> </td> 
<td width='20%'>Ziggy Stardust anniversary to be marked by plaque </td><td width='20%'>BBC OTHERS </td><td width='20%'> 2012-03-10 21:15:46 </td></tr></tbody></table> 
  <input name="save" type="submit" id="save" value="save">
  </form>

<form name="form1" method="" action="show_others.php">
 <input name="Retreat" type="submit" id="Retreat" value="Retreat">
  </form>
0

it's worke :)
thanks alot ,,,

if i want delete ,,
this code
<?php

  $delete = $_POST['delete'];
    $checkbox = $_POST['checkbox'];
    if($delete)
    {for($i=0;$i<$num;$i++){
    $del_id =$checkbox[$i];
    $sql = "DELETE FROM $show_others WHERE id='$del_id'";
    $result = mysql_query($sql);
    }

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

but it's Deleted in order, and not by choice!

0

your solved are right :)

just change from line 20 to line 45 to this

while( $row = mysql_fetch_array($result))  { 
   echo"<tr>";
  ?>
  <form name="form1" method="post" action="">
    <td> <input name="radio[]" type="radio" id="radio[]" value="<?php echo $row['id']?>"> </td> 
  <td ><?php echo $row['topic']; ?></td>
    <td ><?php echo $row['author']; ?></td>
  <td ><?php echo $row['date']; ?></td>

can you help me here :
http://www.daniweb.com/web-development/php/threads/418736/display-information-with-while-and-radio-bottom-

:)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.