0

So I have been toying around with PHP and MySQL for a while now, but I can't get this to work.

I have the following two code snippets.

From index.php:

<form action="send.php" method="post">
    <label>
        <p class="tekst">Nu kommer det svære valg! Skal det være: <br/>
            <input type="radio" name="Tur" id="Tur1" value="Tur1">Tur 1?<br />
        </label> 
        <label>
            <input type="radio" name="Tur" id="Tur2" value="Tur2">Tur 2?</p>
        </label> <br />
        <center> <button type="button" class="btn btn-success btn-lg" onclick="turSvar()">Jeg er klar til gåtur!</button> </center> <br/> <br/>
    </form>

and send.php:

if (isset($_POST['Tur'])){
        $Tur = $_POST['Tur'];
        mysql_query("INSERT INTO whatever VALUES ($Tur)");
    }

I have already created a table called "whatever" in my database (managed through phpMyAdmin). If I use the editor in phpMyAdmin to insert a string to the whatever table, it works. So is there a mistake in any of my code? I use mysqli_connect to connect to the database.

Any help would be nice!

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
user2956248
  • 197
  • 1
  • 1
  • 9
  • Have you tried echoing the query itself, to see if it's being created correctly? Also, are you sure $_POST["Tur"] is set? The form in the html snippet has no submit button, but rather a button that has an onclick function; what does it do? Are you sure the form data is being sent? I suspect that's where the problem lies – Michael Beeson Nov 10 '13 at 22:39
  • do you get an error from MySQL? – Greg Nov 10 '13 at 22:40
  • is your table composed of one column really? – Hilmi Erdem KEREN Nov 10 '13 at 22:41
  • I think you're right Michael Beeson. I'm still new to everything related to web development, so I think it's definitely there the problem lies. I just added a Submit button, and now when I press it I get redirected to "send.php".. Which is a start I guess :) – user2956248 Nov 10 '13 at 22:43
  • As Hilmi says, this relies on only having a single column in the table (its very bad practice to make assumptions like this - and also implies bad data design). It also assumes that the user will submit a numeric value. Further you really should read up on SQL injection. – symcbean Nov 11 '13 at 00:21
  • There's no post field called `Tur` – Ben Nov 11 '13 at 01:39
  • Turn error reporting on! – Ben Nov 11 '13 at 01:41
  • Why do you mix mysqli with mysql??? Use `mysqli_connect()` AND `mysqli_query()` or the ancient and deprecated `mysql_connect()` AND `mysql_query()`! – CodeZombie Nov 11 '13 at 01:49

1 Answers1

1

Of course you need to create a connexion to the database first in order to interact (INSERT/UPDATE/DELETE, etc.) with it:

$conn = mysql_connect( $db_host, $db_user, $db_password );

Now select the database:

mysql_select_db( $db_name );

Please since you are beginning read : Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
Sébastien
  • 11,860
  • 11
  • 58
  • 78