Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正确的php语法_Php_Sql - Fatal编程技术网

正确的php语法

正确的php语法,php,sql,Php,Sql,这一整天都有麻烦,我终于明白了。如果我使用上面的代码,它只会向数据库中添加一篇新文章。这应该用于编辑帖子,因此显然我需要编辑现有信息: // query $sql = "INSERT INTO tool (title,details) VALUES (:title,:details) "; $q = $conn->prepare($sql); $q->execute(array(':details'=>$details, ':title'=&

这一整天都有麻烦,我终于明白了。如果我使用上面的代码,它只会向数据库中添加一篇新文章。这应该用于编辑帖子,因此显然我需要编辑现有信息:

// query
$sql = "INSERT INTO tool (title,details) VALUES (:title,:details) ";
$q = $conn->prepare($sql);
$q->execute(array(':details'=>$details,
                  ':title'=>$title));
“id”是数据库中的一列。我需要它来更新标题和细节,为具体的职位。我只是不确定我应该在这里使用什么语法

谢谢你的回答

====第二个问题:

现在我又回到了我以前的错误。每当我编辑一篇文章时,它只会丢失一次标题和详细信息。第一次编辑帖子时,我丢失了所有的信息,但其余时间它会正常工作。知道为什么吗?代码如下:

编辑页面的表单(可能重要,也可能不重要,我不知道):

$name=$\u会话['Username'];
if(在_数组中($name,$allowed)){
$results=mysql_查询(“从工具中选择*,其中id=$post”);
while($row=mysql\u fetch\u数组($results)){
$title=$row['title'];
$details=$row['details'];
$date=$row['date'];
$author=$row['author'];
$id=$row['id'];
回声“

”; echo$标题; 回声“

”; 回声' '; echo$标题; 回声“
”; ?>
以下是插入数据库的SQL:

$name = $_SESSION['Username'];
if (in_array($name, $allowedposters)) {
$results = mysql_query("SELECT * FROM tool WHERE id = $post");
    while($row = mysql_fetch_array($results)){


$title= $row['title'];
$details= $row['details'];
$date= $row['date'];
$author= $row['author'];
$id= $row['id'];



echo "<a href=story.php?id=";
echo $post;
echo ">Cancel edit</a> <br><br><b>";
echo $title;
echo "</b> <br><br>";
echo '
<form action="edit-new.php?story=';
echo $id;
echo '" method="post" enctype="multipart/form-data">
<textarea rows="1" cols="60" name="title" wrap="physical" maxlength="100">';
echo $title;
echo '</textarea><br>';
?>



<textarea rows="30" cols="60" name="details" wrap="physical" maxlength="10000">
<?php 
echo $details;
echo '</textarea><br>';

echo '<label for="file">Upload featured image:</label><br>
      <input type="file" name="file" id="file" />';
echo'<br><input type="submit" />';
}

} else {
  echo "Not enough permissions.";
}
?>

修复您的sql


信息的“随机性”有多大?抱歉,我在写这部分时没有考虑。请重新阅读。顺便说一句,如果你要发回整个故事,也许一篇帖子比一篇GET更合适,而且(这只是我)我会将参数命名为“storyid”而不是“story”为了避免一点混乱。这是按计划进行的,但现在我又回到了我以前的错误。每当我编辑一篇文章时,它只会丢失一次标题和详细信息。第一次编辑文章时,我会丢失所有信息,但其余时间它会正常工作。知道为什么吗?你的错误在其他地方我想你没有传递e的新值以表格或类似的形式发表的文章。没有代码很难说,可能会问新问题。
$name = $_SESSION['Username'];
if (in_array($name, $allowedposters)) {
$results = mysql_query("SELECT * FROM tool WHERE id = $post");
    while($row = mysql_fetch_array($results)){


$title= $row['title'];
$details= $row['details'];
$date= $row['date'];
$author= $row['author'];
$id= $row['id'];



echo "<a href=story.php?id=";
echo $post;
echo ">Cancel edit</a> <br><br><b>";
echo $title;
echo "</b> <br><br>";
echo '
<form action="edit-new.php?story=';
echo $id;
echo '" method="post" enctype="multipart/form-data">
<textarea rows="1" cols="60" name="title" wrap="physical" maxlength="100">';
echo $title;
echo '</textarea><br>';
?>



<textarea rows="30" cols="60" name="details" wrap="physical" maxlength="10000">
<?php 
echo $details;
echo '</textarea><br>';

echo '<label for="file">Upload featured image:</label><br>
      <input type="file" name="file" id="file" />';
echo'<br><input type="submit" />';
}

} else {
  echo "Not enough permissions.";
}
?>
<?php
   $post = htmlspecialchars($_GET['story']);
   $title = mysql_real_escape_string($_POST['title']);
   $details = mysql_real_escape_string($_POST['details']);
      echo "B<br>";
      echo $_POST['title'];
      echo '<br>';
      echo $_POST['details'];
      echo $post;
      echo "<br><br>";


// configuration
$dbtype     = "mysql";
$dbhost     = "localhost";
$dbname     = "zzzz";
$dbuser     = "zzzzz";
$dbpass     = "zzzzzz";

// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

// new data


// query
$sql = "UPDATE  tool SET title=:title, details=:details WHERE id = :postid";
$q = $conn->prepare($sql);
$q->execute(array(
  ':details'=>$details,
  ':title'=>$title,
  ':postid' => $post
));




?>
$sql = "UPDATE  tool SET title=:title, details=:details WHERE id = :postid";
$q = $conn->prepare($sql);
$q->execute(array(
  ':details'=>$details,
  ':title'=>$title,
  ':postid' => $post
));