update code was falsch???

Kimble

Erfahrenes Mitglied
hi,
was ist an folgendem Code falsch?
Ich uebergeb per $_POST die Variablen aber es kommt immer eine Fehlermeldung:
PHP:
Error while updating news: You have an error in your SQL syntax near 'WHERE id=11' at line 1

un hier der code:
ich weiss wirklich nicht was an dem SQL syntax falsch ist :(
Bitte helft!
PHP:
<?php
    $dbcnx = mysql_connect( "localhost", "Jan Fischer", "ibidcset" );
    mysql_select_db( "clan" );

    if( $send != "save" ) {
	$sql = mysql_query("SELECT createdby, email, topic, inhalt, ndate, ntime, comments FROM " .
		"cms_news WHERE id=$id");
	$sql = mysql_fetch_array( $sql );
    ?>
	<html>
	<head>
	    <title>CMS System</title>
	    <link rel="stylesheet" type="text/css" href="style/color.css" />
	</head>

	<body>

	<div align="center">
	<p><b><u>Edit: <?=$sql['topic']?></u></b></p>
	<form action="<?=$PHP_SELF?>" method="post" enctype="multipart/form-data">
	<table width="500" border="0" cellpadding="3" cellspacing="1">
	    <tr>
		<th width="100" align="left">created by</th>
		<td class="view">
		    <input name="createdby" maxlength="50" size="50" value="<?=$sql['createdby']?>" />
		</td>
	    </tr>
	    <tr>
		<th width="100" align="left">email</th>
		<td class="view"><input name="email" maxlength="100" size="50" value="<?=$sql['email']?>" /></td>
	    </tr>
	    <tr>
		<th width="100" align="left">topic</th>
		<td class="view"><input name="topic" maxlength="50" size="50" value="<?=$sql['topic']?>" /></td>
	    </tr>
	    <tr>
		<th width="100" align="left">content</th>
		<td class="view"><textarea rows="5" name="inhalt" cols="50"><?=$sql['inhalt']?></textarea></td>
	    </tr>
	    <tr>
		<th width="100" align="left">date</th>
		<td class="view"><input name="ndate" size="50" value="<?=$sql['ndate']?>" /></td>
	    </tr>
	    <tr>
		<th width="100" align="left">time</th>
		<td class="view"><input name="ntime" size="50" value="<?=$sql['ntime']?>" /></td>
	    </tr>
	    <tr>
		<th width="100" align="left">comments</th>
		<td class="view"><?=$sql['comments']?>&nbsp;&nbsp;- can not be edited
		<input type="hidden" size="1" name="id" value="<?=$id?>" /></td>
	    </tr>
	    <tr>
		<td colspan="2" align="center" class="view">
		    <input type="submit" name="send" value="save" />&nbsp;&nbsp;
		    <input type="reset" name="reset" value="reset" />
		</td>
	    </tr>
	</table>
	</form>
	</div>

	</body>
	</html>
<?php
    } else {
	$sql = "UPDATE cms_news SET createdby='$_POST[createdby]', email='htmlspecialchars($_POST[email])', " . 
	    "topic='$_POST[topic]', inhalt='$_POST[inhalt]', ndate='$_POST[ndate]', " .
	    "ntime='$_POST[ntime]', WHERE id=$_POST[id]";
	
	mysql_query( $sql ) or die("Error while updating news: " . mysql_error());

	header("Location: index.php");
	exit();
    }
?>
 
Zuletzt bearbeitet:
Code:
"UPDATE cms_news SET createdby='$_POST[createdby]', email='htmlspecialchars($_POST[email])', " . 
        "topic='$_POST[topic]', inhalt='$_POST[inhalt]', ndate='$_POST[ndate]', " .
        "ntime='$_POST[ntime]', WHERE id=$_POST[id]";

hier liget der Fehler!
Wenn du die Rückgabewerte von Funktionen in Variablen einbinden willst, musst du sie per Punktoperator (.) verbinden).
Ausserdem sollte man das auch bei Arrays machen
Also so müsste es richtig sein:

PHP:
"UPDATE cms_news SET createdby='".$_POST[createdby]."', email='".htmlspecialchars($_POST[email])."', " . 
        "topic='".$_POST[topic]."', inhalt='".$_POST[inhalt]."', ndate='".$_POST[ndate]."', " .
        "ntime='".$_POST[ntime]."', WHERE id=".$_POST[id];
 
dann versuchs mal damit:

PHP:
"UPDATE cms_news SET createdby='".$_POST["createdby"]."', email='".htmlspecialchars($_POST["email"])."', " . 
        "topic='".$_POST["topic"]."', inhalt='".$_POST["inhalt"]."', ndate='".$_POST[ndate]."', " .
        "ntime='".$_POST["ntime"]."', WHERE id=".$_POST["id"];
 
Dann würd ichs mal per Hand in phpMyAdmin mahcne und da den Befehl rauskopieren und bearbeiten!

Sorry
 
PHP:
$sql = mysql_query("SELECT createdby, email, topic, inhalt, ndate, ntime, comments FROM cms_news WHERE id='$id'");
 
Probier mal, die WHERE-Anweisung in Hochstriche zu schreiben, vielleicht geht das.

Feldhofe

edit: wieder mal zu lahm -genau wie mein Vorgänger meine das ich auch!
Warum machst du eigentlich bei jedem Zeilenumbruch ein " . " rein??
 
Zuletzt bearbeitet:
In dem Buechern, die ich hab wird das so gemacht.
Und aus Buechern lernt man ja

kommt auf den verlag an ich habe noch kein buch ohne fehler gesehen.. und wenns data becker ist stimmt eh nur 50% :-)

ansonsten sieht das von Typohnename gut aus
aber hinten fehlt noch was

... WHERE id='".$_POST["id"]."'";
 
Zuletzt bearbeitet:
Zurück