-1

I would like these values to go into my database but it just won't do it.

Form Code

<form action='ryge.php' method='POST'>
  <p> Navn: </p> <p><?php echo $_SESSION['first'] . " " . $_SESSION['last'] ?></p> <br>
<input type='text' name='name' placeholder='dit navn'> <br>
<input type='text' name='cig' placeholder='Navn på smøger'> <br>
<input type='text' name='brand' placeholder='Navn på brandet af smøger?'> <br>
<input type='text' name='unit' placeholder='Hvor mange enheder har du?'> <br>
<input type='text' name='pris' placeholder='Pris pr smøg'> <br>
<input type='text' name='lighter' placeholder='Har du lighter'> <br>
<input type='text' name='place' placeholder='Lokation til rhyyge'> <br>
<input type='text' name='tid' placeholder='Hvad tid passer dig bedst?'> <br>
<input type='text' name='howlong' placeholder='Hvor langtid har du at rhyyge i?'> <br>
<input type='text' name='day' placeholder='hvilke dag/dage'> <br>
<input type='text' name='pers' placeholder='Alene eller i gruppe?'> <br>
<button type='submit' name='tilføj' >Tilføj til dine præferencer broder</button>
</form>

PHP Code

<?php
session_start();
include 'dbh.php';

    $name = $_POST['name'];
    $cig = $_POST['cig'];
    $brand = $_POST['brand'];
    $unit = $_POST['unit'];
    $pris = $_POST['pris'];
    $ligther = $_POST['ligther'];
    $place = $_POST['place'];
    $tid = $_POST['tid'];
    $howlong = $_POST['howlong'];
    $day = $_POST['day'];
    $pers = $_POST['pers'];

    $sql = "INSERT INTO prod (name, cig, brand, unit, pris, lighter, place, tid, howlong, day, pers) 
            VALUES ('$name', '$cig', '$brand', '$unit', '$pris', '$ligther', '$place', '$tid', '$howling', '$day', '$pers')";
    $result = mysqli_query($conn, $sql);

header("Location: index.php");
akash
  • 22,664
  • 11
  • 59
  • 87

1 Answers1

0

You are on the right track but really need to refine your code. First, the SQL syntax you are using is out of date and susceptible to injection. You should read about prepared statements and make that your way of coding in the future.

Second, as one of the other posters mentioned, you are including the same file more than once.

Third, you should also look at the ISSET command and include it via an if / else statement so that your code runs properly. Right now, you are running the entire code on the server with no data to fill it. You need to look for a proper "Submit" via ISSET and execute the PHP code at that point.

See here for more help:

how to insert into mysql using Prepared Statement with php

Community
  • 1
  • 1