Jump to content

Formular help?


calle08

Recommended Posts

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>

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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'";

 

Link to comment
Share on other sites

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')");
}
?>

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.