0

I have a HTML/PHP(MySql) related question

I just have troubles finding out how to pass more than one array to the same row in a database

Code: (- at the start of the page)

   <?php
    if ( isset($_POST['logActivity']) ) {

        $date = trim($_POST['date']);
        $date = strip_tags($date);
        $date = htmlspecialchars($date); //Aquiring a date() function variable

    foreach ($_POST['activity'] as $activity => $value){
$query="INSERT INTO monitorlog(dateofentry,activity)

VALUES ('$date','$value')";
mysqli_query($conn,$query);
}} ?>

(In the < body> section)

<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" >
<table>
<tr>
    <td width='100px' align='center'><input width='100%' value='" . $activity . "' name='activity[]' type='text' class='form-control' /></td><td>
    <input width='100%' value='" . $activity2 . "' name='activity2[]' type='number' class='form-control' /></td>
    </tr><tr>
    <td width='100px' align='center'><input width='100%' value='" . $activity . "' name='activity[]' type='text' class='form-control' /></td><td>
 <input width='100%' value='" . $activity2 . "' name='activity2[]' type='number' class='form-control' /></td>
    </tr><tr>
    <td width='100px' align='center'><input width='100%' value='" . $activity . "' name='activity[]' type='text' class='form-control' /></td><td>
    <input width='100%' value='" . $activity2 . "' name='activity2[]' type='number' class='form-control' /></td>
</tr><tr><td>
<button type='submit' class='mybuttons' name='button'>Log to database</button>
</td></tr>
</table>
    </form>

The code above successfully inputs the data of $activity in the database but it also limits the value of the input to $activity's value on that particular row in the database, whereas I need to pass both activity and activity2 to the same row. Feels like I've tried everything by now to no effect. Hope you guys can help me

Best regards.

fisk
  • 1
  • 1
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Oct 03 '17 at 15:48
  • I think you loop into one array in your code... you don't run with "activity2" in $_POST... But, you'll need to insert both activity and activity2 in the activity column of your mySQL table ? – Jean-Luc Aubert Oct 03 '17 at 15:50

0 Answers0