0

I am stuck in a very crucial part of my project and would like some help - however I seem to be stuck in the PHP / SQL syntax and cannot get the query to work.

HTML code:

<form name="homepage" method="POST" action="" >
   <p>Page Title</p>
  <input id="pagetitle" type="text" name="home_title" value="<?php select_text("SELECT fieldcontent FROM content WHERE name='home_title'", "fieldcontent") ?>"/>
    <p>Paragraph</p>
    <textarea id="paragraph" name="home_text"><?php select_text("SELECT fieldcontent FROM content WHERE name='home_text'", "fieldcontent") ?>  </textarea>
    <h1>Images</h1>
    <div id="image">
    <?php select_image("SELECT * FROM `image` WHERE image_cat_id = 8"); ?>
    </div>
    <button name="homesavebtn" id="home-save-btn" type="submit">Save Updates</button>
</form>

PHP code - Select data

function select_text($sql, $echo) {
include 'connect.php';

$result = $conn->query($sql);
if ($result->num_rows > 0);
while ($row = $result->fetch_assoc()) {
    echo $row[$echo];
    $conn->close(); 
  } 
}

PHP code - update

if ($_POST) {
if (isset($_POST['homesavebtn'])){
$home_title = (isset($_POST['home_title']) ? $_POST['home_title'] : null);
$home_text = (isset($_POST['home_text']) ? $_POST['home_text'] : null); 

include 'connect.php';

 $sql = "INSERT INTO content(name, fieldcontent) VALUES ('home_title', '$home_title') ON DUPLICATE KEY UPDATE fieldcontent = '$home_title'"; 
    $sql .= "INSERT INTO content(name, fieldcontent) VALUES ('home_text', '$home_text') ON DUPLICATE KEY UPDATE fieldcontent = '$home_text'";

if (mysqli_query($conn, $sql)) {
echo "";
} else {
echo "" . $sql . "<br>" .mysqli_error($conn);
} 
$conn->close();

}
}

Getting the following error:

INSERT INTO content(name, fieldcontent) VALUES ('home_title', 'Mosta Cycling Club') ON DUPLICATE KEY UPDATE fieldcontent = 'Mosta Cycling Club'INSERT INTO content(name, fieldcontent) VALUES ('home_text', '') ON DUPLICATE KEY UPDATE fieldcontent = '' 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 'INSERT INTO content(name, fieldcontent) VALUES ('home_text', '') ON DUPLICATE KE' at line 1

1
  • Please add a description of the problem you are having - what specifically is going wrong with what you have at the moment? Commented Jun 1, 2015 at 18:09

2 Answers 2

1

You could use VALUES to get the new value you are using in the update portion. Also, if you use prepare and bind_param you will prevent SQL injection:

$mysqli = new mysqli('host', 'user', 'password', 'db');

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$stmt = $mysqli->prepare("INSERT INTO content(name, fieldcontent) 
                          VALUES ('home_title', ?), ('home_text', ?)
                          ON DUPLICATE KEY UPDATE fieldcontent = VALUES(fieldcontent)");

$stmt->bind_param('ss', $home_title, $home_text);
$stmt->execute();
Sign up to request clarification or add additional context in comments.

3 Comments

Mysqli needs a type argument to bind_param, doesn't it?
What is the meaning of 'ss' in $stmt->bind_param('ss', $home_title, $home_text); ?
Each s says the type of a parameter is a string
0

Your second SQL statement is being added to your first creating one long statement that doesn't make sense. Separate these into two different statements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.