0

I have a database with 3. The first tables are named "periorismoi" and "programmata". The first one has 2 colums ("Auxon" , "Title") and the second one 3 ("Auxon" , "Name" , "Periorismoi"). I made a page with a form of adding a new 'programmata'. In the column "Name" a text will be added from the form and in the column "Periorismoi" a name will be added from a checkbox (from the same form). The checkbox names will derive from the table "periorismoi". I am giving you the page of the form and then the Insert to the database (action) page. It doesn't work. Maybe a server error ? It used to work before I put the checkboxes in the form. Note that Periorismoi is different than periorismoi.

   <form action='Insert.php' method="post" enctype='multipart/form-data' periorismoi="periorismoi">
   <textarea name="title" placeholder="Πρόγραμμα" rows=\"1\" cols=\"50\"></textarea>
<?php
$link = mysql_connect('localhost', 'student1905','123456');
mysql_select_db('student1905');
$q="select * FROM periorismoi";
$result=mysql_query($q);
$counts=mysql_num_rows($result);
for ( $i = 0; $i < $counts; ++$i ) {
$row = mysql_fetch_array( $result );
    if(isset($row[ "Title" ]))
   {$Title= $row[ "Title" ];
     }
    ?> 
<input type="checkbox" name="<?php echo "periorismoi$i"; ?>" value=""<?php echo $row['Title']; ?>""><?php echo "$Title";?>      <br>      
<?php
}
?>

Insert.php page:

<?php
$link = mysql_connect('localhost', 'student1905','123456');
mysql_select_db('student1905');
$title= trim($_POST["title"]);
if(!$link) 
{
   die('Could not connect: ' . mysql_error());
}
else {
    if(strlen($title) == 0) 
    {
        header("Location:Index.php"); 
    }
    else
    {
     mysql_query("SET NAMES utf8");
      $periorismoi = implode(',', $_POST['periorismoi']);
    $len = sizeof($periorismoi);
    for($i=0; $i<$len;$i++){ 
        $sql="INSERT INTO programmata (Auxon,Title,Periorismoi) VALUES ('','$title','$periorismoi')";
                 mysql_query($sql);
    }
      header("Location:Index.php"); ; 
    }
}
?>
3
  • do you get any error message? besides that please pay attention to possible sql injection attacks in your code. Commented Oct 2, 2013 at 20:51
  • @TobiasZander No error shown. It worked before connecting the checkboxes to the table "periorismoi". Commented Oct 2, 2013 at 20:59
  • @TobiasZander How do i check sql injection attacks ? I mean how do I avoid them? Commented Oct 2, 2013 at 21:23

1 Answer 1

2

In the form, you create multiple checkboxes with different name attributes:

(periorismoi0, periorismoi1, ...)

Then when you read your parameters you are trying to read $_POST['periorismoi'] which doesn't exist. You should either read every single checkbox ($_POST['periorismoi0'], $_POST['periorismoi1'], ... (which I highly don't recommend) or you should define multiple checkboxes with the same name.

Check this thread: Get $_POST from multiple checkboxes

Sign up to request clarification or add additional context in comments.

1 Comment

I wish I could give you reputation. You helped me so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.