calle08 Posted August 31, 2011 Share Posted August 31, 2011 Using this form to add dates to my database. Getting error on line 14. Don't know the right syntax somone can help me get it to work? This line in the form don,t work. Posted all the form code under... $laggtill = "insert into narvaro_3manna (fornamn, efternamn, narvaro_id, datum) SELECT fornamn, efternamn, narvaro_id, (datum) VALUES ('$datum') WHERE datum = '2011-09-01'"; <code> <?php include "anslut.php"; // Databasanslutningen //Här uppdaterar tid IF($_GET['do']=='sparatid' && $_POST['datum']!=''){ $info=$_POST['info']; $uppdatera = "UPDATE narvaro_3manna SET info = '{$info}' WHERE datum = '{$datum}'"; $res = mysql_query($uppdatera) or die("SQL: Gick ej att uppdatera informationen $uppdatera <br>".mysql_error()); } //Här sparas nya ElseIF($_GET['do']=='nytid'){ $datum=$_POST['datum']; $laggtill = "insert into narvaro_3manna (fornamn, efternamn, narvaro_id, datum) SELECT fornamn, efternamn, narvaro_id, (datum) VALUES ('$datum') WHERE datum = '2011-09-01'"; $res = mysql_query($laggtill) or die('SQL: Gick ej att lägga till informationen $laggtill <br>'.mysql_error()); } //Här raderas tid ElseIF($_GET['do']=='raderatid' && $_GET['datum']!=''){ $datum=$_GET['datum']; $radera = "DELETE FROM narvaro_3manna WHERE datum = '$datum'"; mysql_query($radera) or die('SQL: Gick ej att radera informationen: $radera <br>'.mysql_error()); } //Här visas narvaro_3mannaa tid $sql = "SELECT DISTINCT datum, color, info FROM narvaro_3manna WHERE datum >= CURDATE( ) ORDER BY datum ASC"; $resultat = mysql_query($sql); echo "<table border ='0'>\n"; echo " <tr>\n"; echo " </tr>\n"; echo " <tr><form method='POST' action='?do=nytid'>\n"; echo " <td><input type='text' name='datum' size='10'></td>\n"; echo " <td><input type='submit' value='Spara nya' name='submitny'></td>\n"; echo " </form></tr>\n"; while($kolumn = mysql_fetch_array($resultat)){ echo " <tr><form method='POST' action='?do=sparatid'>\n"; echo " <input type='hidden' name='datum' value='{$kolumn['datum']}'>\n"; echo ' <td>'.$kolumn['datum'].'</td>'; echo " <td><input type='text' name='info' size='150' value='{$kolumn['info']}'>"; echo " <td><input type='submit' value='Spara' name='submit{$kolumn['datum']}'>"; echo " <a href='?do=raderatid&datum={$kolumn['datum']}'>Radera</a></td>\n"; echo " </form></tr>\n"; } echo "</table>\n"; ?> </code> Quote Link to comment https://forums.phpfreaks.com/topic/246123-formular-help/ Share on other sites More sharing options...
micah1701 Posted August 31, 2011 Share Posted August 31, 2011 i'm not sure i exactly get what your code is trying to do but proper "INSERT" format for an SQL statement is: INSERT INTO `table_name` (list,of,columns,to,update) VALUES ('string','text','for','$each','column') there shouldn't be a WHERE clause or anything else unless you really want to UPDATE a row? then you want: UPDATE `table_name` SET column_name = 'string value', column2 = 'value', you_get = 'the idea' WHERE some_column = 'some_value' Quote Link to comment https://forums.phpfreaks.com/topic/246123-formular-help/#findComment-1263983 Share on other sites More sharing options...
samshel Posted August 31, 2011 Share Posted August 31, 2011 Whats the exact error you are getting ? Quote Link to comment https://forums.phpfreaks.com/topic/246123-formular-help/#findComment-1264055 Share on other sites More sharing options...
calle08 Posted August 31, 2011 Author Share Posted August 31, 2011 I want this question in the form. 2011-09-02 is the date that is added from the form. insert into narvaro_3manna (fornamn, efternamn, narvaro_id, datum) SELECT fornamn, efternamn, narvaro_id, '2011-09-02' FROM narvaro_3manna WHERE datum ='2011-09-01' Here is the line where i want the question. But getting syntax errors. $laggtill = "insert into narvaro_3manna (fornamn, efternamn, narvaro_id, datum) SELECT fornamn, efternamn, narvaro_id, (datum) VALUES ('$datum') WHERE datum = '2011-09-01'"; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2011-09-02')' WHERE datum = '2011-09-01'' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/246123-formular-help/#findComment-1264078 Share on other sites More sharing options...
samshel Posted August 31, 2011 Share Posted August 31, 2011 Not sure what you are trying to do here $laggtill = "insert into narvaro_3manna (fornamn, efternamn, narvaro_id, datum) SELECT fornamn, efternamn, narvaro_id, (datum) VALUES ('$datum') WHERE datum = '2011-09-01'"; Do you want to insert 1 record or all records for that date from some table into narvaro_3manna ? Quote Link to comment https://forums.phpfreaks.com/topic/246123-formular-help/#findComment-1264079 Share on other sites More sharing options...
calle08 Posted September 1, 2011 Author Share Posted September 1, 2011 Yes all records for that date from some table into narvaro_3manna Quote Link to comment https://forums.phpfreaks.com/topic/246123-formular-help/#findComment-1264184 Share on other sites More sharing options...
calle08 Posted September 1, 2011 Author Share Posted September 1, 2011 I want to copy all the lines from 2011-09-01 and change the date to ex, 2011-09-02 i need to use the insert into line to make it work. But it may not work att all like this? Someone know what i can do to make it work? $laggtill = "insert into narvaro_3manna (fornamn, efternamn, narvaro_id, datum) SELECT fornamn, efternamn, narvaro_id, (datum) VALUES ('$datum') WHERE datum = '2011-09-01'"; Quote Link to comment https://forums.phpfreaks.com/topic/246123-formular-help/#findComment-1264338 Share on other sites More sharing options...
micah1701 Posted September 1, 2011 Share Posted September 1, 2011 ah, I see. Yeah, i've run into this before where you want to basically copy a row but change one thing about it. Usually when you ask this on a message board people make fun of you and say that means you have a poorly designed database or something... but I get what you're trying to do. I'm not familiar with a specific SQL function in MySQL to "copy" a row (although there may be something and I just don't know it) Your best bet is probably to use a combo of PHP and multiple queries like: <?php // this is untested code, just a suggestion of how you could do this... $sql = "SELECT * FROM table_name WHERE datum = '2011-09-01' "; $results = mysql_query($sql); foreach(mysql_fetch_assoc($results) as $result){ mysql_query("INSERT INTO table_name (column1,column2,datum) VALUES ('$result[column1]','$result[column2]','2011-09-02')"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246123-formular-help/#findComment-1264353 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.