Jump to content

simple INSERT INTO problem


Yawa

Recommended Posts

I get the $err msg. in my script. But I can't seem to se the problem my self.

Can you fint it?

if ($_POST['submit_new_album'])
{
  if (!val_alb_name($_POST['name'])) {}

  else
  {
    $date = mysql_real_escape_string($_POST['date']);
    $name = mysql_real_escape_string($_POST['name']);
    $desc = mysql_real_escape_string($_POST['desc']);

    $query = sprintf ("INSERT INTO album (date, name, desc) VALUES ('$date', '$name', '$desc')");
    $result = mysql_query ($query);  //  Her stopper det tipper jeg

    if (!$result) { $err = '[NOR]Beklager, men en feil oppstod ved skriving til databasen'; }  //  Here is the error-message i get.
    else { $ok = '[NOR]Ett nytt album er blitt opprettet for deg: '. $_POST['name']; }
  }
}
else {}

 

And here is the form:

<?php include ('/path/to/script/gallery.php'); ?>
<h1>[NOR]Opprett ett nytt album</h1>
<p><?php print '<strong><span class="green">'. $ok .'</span><span class="red">'. $err .'</span></strong>'; ?></p>
<form name="new_alb" method="post" action="?gdc=galleri&funksjon=nytt-album">
  <table>
    <tr><td></td><td><input name="date" type="hidden" value="20<?php print date ('y-m-d'); ?>" /></td></tr>
    <tr><td>Album-navn:</td><td><input name="name" type="text" size="80" /></td></tr>
    <tr><td></td><td><textarea name="desc" cols="80" rows="5" ></textarea></td></tr>
    <tr><td></td><td><input name="submit_new_album" type="submit" value="opprett"/></td></tr>
  </table>
</form>

 

I'm using mySQL.

Link to comment
Share on other sites

Not sure if it is the same; however, I spent a good hour last night with insert problem - I was using the word "when" as a field name. It seems "when" is a reserved word. The same may be true of "desc" as that is used to determine ORDER BY

Link to comment
Share on other sites

Break down

 

$query = sprintf ("INSERT INTO album (date, name, desc) VALUES ('$date', '$name', '$desc')");
    $result = mysql_query ($query);  //  Her stopper det tipper jeg

 

to

 

$query = "INSERT INTO album (date, name, desc) VALUES ('$date', '$name', '$desc')";

print $query;

$result = mysql_query ($query);  //  Her stopper det tipper jeg

 

This will allow you to look at the query statement to ensure that it is correct and to run it directly against the database.  Also, why are you using sprintf ?

Link to comment
Share on other sites

I use sprintf() because of the mysql_real_escape_string(). Isn't that correct? Always used this method. Is ther a better/another way?

 

This is what i got printet out, and it seems correct.

INSERT INTO album (date, name, desc) VALUES ('2010-05-04', 'Testing', 'Testing...')

 

I have used these names in other tables aswell, never been a problem. I have several scripts in the same document. But i have done this for lot of documents. In my whole backend

Link to comment
Share on other sites

it is very strange because. I have set up this script right above. and it works quite ok:

 

if (isset($_POST['submit_upl_img']))
{
  if (!val_img($_FILES['img']['name'], $_FILES['img']['tmp_name']) || !val_alt($_POST['alt'])) {}

  else
  {
    $sql_ext = mysql_real_escape_string($fileext);
    $date = mysql_real_escape_string($_POST['date']);
    $alt = mysql_real_escape_string($_POST['alt']);
    $album = mysql_real_escape_string($_POST['album']);

    $query = sprintf("INSERT INTO gallery_picture (ext, date, alt, album_id) VALUES ('$sql_ext', '$date', '$alt', '$album')");
    $result = mysql_query($query);

    if (!$result) { $err = 'OBS! Something went wrong when writing to the database'; }

    else
    {
      $filename = mysql_insert_id();
      $query = mysql_query('SELECT * FROM gallery_picture WHERE id = "'. $filename .'"');
      $getfi = mysql_fetch_assoc($query);

      if (!create_thumb($getfi['album_id'], $getfi['id'], $fileext, $_FILES['img']['tmp_name']))
      {
        mysql_query('DELETE FROM gallery_picture WHERE id = "'. $filename .'"');
        $err = 'OBS! Something went wrong when uploading the prictures.';
      }
      else
      {
        $ok = 'Picture was uploaded successfully ID: '. $getfi['album_id'] . $getfi['id'];
      }
    }
  }
}
else {}

 

Togehter with all the functions...

I tried to put the functions and the script in another document. but the problem is still there.

I tried to create a new table in the database with no luck. So i'm stuck here.

By the way, I have set up several others querys later that works fine. The same way. can't see the difference between them.

 

I could post them if that would help...

My host is bluehost.com.

They couldn't help me with programming questions

Link to comment
Share on other sites

Not sure if it is the same; however, I spent a good hour last night with insert problem - I was using the word "when" as a field name. It seems "when" is a reserved word. The same may be true of "desc" as that is used to determine ORDER BY

 

I thought i had used "desc" before. but it turned out that I have had the same problem then.

I changed it, and now it works perfectly fine.

 

Thank's for the tip.

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.