Jump to content

verry simple, but cant find my mistake :/


loxfear
Go to solution Solved by Psycho,

Recommended Posts

heres my code:

 

<?php
$con=mysqli_connect("______________________");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
 
$sql = "INSERT INTO aktiviteter (title, firma, beskrivelse, information, pris, rabat, adresse, by, postnummer, telefon, hjemmeside)
VALUES
('$_POST[title]','$_POST[firma]','$_POST[beskrivelse]','$_POST[information]','$_POST[pris]','$_POST[rabat]','$_POST[adresse]','$_POST[by]','$_POST[postnummer]','$_POST[telefon]','$_POST[hjemmeside]')";
 
if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "Thank you for your participation!";
 
mysqli_close($con);
?>
 
 
 
for some reason it tells me that theres an error, and its not the connection to the server :/
Link to comment
Share on other sites

  • Solution

The word 'by' is a MySQL Reserved word. You can't reference it in your query without specifically identifying it as a field name (i.e. backticks).

 

But, that the only thing that jumps out at me. Since you were didn't take the time to even supply the error it could be that other errors exist.

 

Fixed the reserved word issue and the SQL Injection hole

 

 

$con = mysqli_connect("______________________");
 
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
 
$sql = sprint_f(
       "INSERT INTO aktiviteter
            (`title`, `firma`, `beskrivelse`, `information`, `pris`, `rabat`,
             `adresse`, `by`, `postnummer`, `telefon`, `hjemmeside`)
        VALUES
            ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
        mysqli_real_escape_string($con, $_POST['title']),
        mysqli_real_escape_string($con, $_POST['firma']),
        mysqli_real_escape_string($con, $_POST['beskrivelse']),
        mysqli_real_escape_string($con, $_POST['information']),
        mysqli_real_escape_string($con, $_POST['pris']),
        mysqli_real_escape_string($con, $_POST['rabat']),
        mysqli_real_escape_string($con, $_POST['adresse']),
        mysqli_real_escape_string($con, $_POST['by']),
        mysqli_real_escape_string($con, $_POST['postnummer']),
        mysqli_real_escape_string($con, $_POST['telefon']),
        mysqli_real_escape_string($con, $_POST['hjemmeside'])
        );
 
if (!mysqli_query($con, $sql))
{
    die('Error: ' . mysqli_error($con));
}
echo "Thank you for your participation!";
 
mysqli_close($con);
Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.