Yawa Posted May 4, 2010 Share Posted May 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200708-simple-insert-into-problem/ Share on other sites More sharing options...
litebearer Posted May 4, 2010 Share Posted May 4, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/200708-simple-insert-into-problem/#findComment-1053219 Share on other sites More sharing options...
siric Posted May 4, 2010 Share Posted May 4, 2010 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/200708-simple-insert-into-problem/#findComment-1053221 Share on other sites More sharing options...
Yawa Posted May 4, 2010 Author Share Posted May 4, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/200708-simple-insert-into-problem/#findComment-1053249 Share on other sites More sharing options...
Zane Posted May 4, 2010 Share Posted May 4, 2010 I'm stumped.. Try a stricter condition maybe?.. either of the below if ($result === false) if (!$result === true) or turn on error reporting and display errors. Quote Link to comment https://forums.phpfreaks.com/topic/200708-simple-insert-into-problem/#findComment-1053257 Share on other sites More sharing options...
Yawa Posted May 4, 2010 Author Share Posted May 4, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/200708-simple-insert-into-problem/#findComment-1053286 Share on other sites More sharing options...
Yawa Posted May 4, 2010 Author Share Posted May 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200708-simple-insert-into-problem/#findComment-1053323 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.