0

I am trying to display a Website Title on my Home Page. This website title is stored in the database named mywebsite and in table settings. I want to update this with an input type text's value. The title is displayed perfectly but when I write something in the text field and submit it, the database doesn't update. I think I am doing everything right and there isn't any error displaying on my page, but still it is not working. Can anyone figure out the error?
Here's my code:

<?php
// Some database detail
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'mywebsite';
// Making connection
$con = mysqli_connect($host, $username, $password, $database);

// Making a sql query for "Website Title" and saving it in variable $query
$query = "SELECT * FROM settings WHERE NameOfSetting='Website Title'";

// Applying query
$result = mysqli_query($con, $query);

// Fetching data from database
$row = mysqli_fetch_array($result);

if (isset($_POST['submit'])) {
    $title = $_POST['text'];
    mysqli_query($con, "UPDATE settings SET TheSetting=$title WHERE NameOfSetting='Website Title'");
}
?>

<h1><?php echo $row['TheSetting']; ?></h1>

<form method="POST">
    <input type="text" placeholder="Change the title" name="text">
    <input type="submit" name="submit">
</form>

EDIT: When I enter any numbers in the field and then submit and refresh it works fine but it's only working with numbers not with alphabets. I don't know why?

3
  • 2
    SET TheSetting='$title' Commented Mar 14, 2014 at 3:50
  • You are Selecting before Updating, so <?php echo $row['TheSetting']; ?> will always be the old value, not the new value. Move your UPDATE before your SELECT Commented Mar 14, 2014 at 3:53
  • 1
    @Fred-ii- You are the boss you got the problem thanks. Commented Mar 14, 2014 at 3:53

4 Answers 4

1

This line:

SET TheSetting=$title

$title needs to be wrapped in quotes:

SET TheSetting='$title'

Sidenote: You may also want to change this line (as a security precaution):

$title = $_POST['text'];

to:

$title = mysqli_real_escape_string($con,$_POST['text']);
Sign up to request clarification or add additional context in comments.

4 Comments

Hey I'm a beginner. And trying to figure out how cms's are made. Would you guide me. Is this the concept of how permenant changes are made through front-end? Or is it a bad practice? Please guide me I am 15 and started learning php!
Oh Lordy, wish I could help but I know next to nothing about CMS's @user41510 I hard code everything I do.
Plus, seeing that this solved your problem, do mark as accepted by clicking the White checkmark till it turns Green. @user41510 so we can close the question.
To somewhat answer your question, that is how SQL works. Once the DB has been (successfully) updated, it's done until it's updated again, so basically that's how it's done. @user41510 You could also look into prepared statements which is a more secure method.
0

Try with

mysqli_query($con, "UPDATE settings SET TheSetting='$title' WHERE NameOfSetting='Website Title'");

1 Comment

Hey I'm a beginner. And trying to figure out how cms's are made. Would you guide me. Is this the concept of how permenant changes are made through front-end? Or is it a bad practice? Please guide me I am 15 and started learning php!
0

Well you can always do some sort of error checking. I.e. using or die(mysqli_error);

$con = mysqli_connect($host, $username, $password, $database)or die(mysqli_error);

This will atleast give you an idea of what your proplem is. Use this error checking method every time you connect, query, or close a database.

3 Comments

Sorry, just trying to help.
I understand. You're presently mixing mysqli_* with mysql_* --- mysql_error should read as mysqli_error ;-)
note - mysqli_error requires the connection link identifier -> mysqli_error($con) php.net/manual/en/mysqli.error.php
0

use this code it will solve your problem.

  <?php
    // Some database detail
    $host = 'localhost';
    $username = 'root';
    $password = '';
    $database = 'mywebsite';
    // Making connection
    $con = mysqli_connect($host, $username, $password, $database)or die(mysqli_error());

    // Making a sql query for "Website Title" and saving it in variable $query
    $query = "SELECT * FROM settings WHERE NameOfSetting='Website Title'";

    // Applying query
    $result = mysqli_query($con, $query);

    // Fetching data from database
    $row = mysqli_fetch_array($result);

    if (isset($_POST['submit'])) {
        $title = $_POST['text'];
        mysqli_query($con, "UPDATE settings SET TheSetting='".$title."' WHERE NameOfSetting='Website Title'");
    }
    ?>

    <h1><?php echo $row['TheSetting']; ?></h1>

    <form method="POST">
        <input type="text" placeholder="Change the title" name="text">
        <input type="submit" name="submit">
    </form>

4 Comments

This is still incorrect. "UPDATE settings SET TheSetting=".$title." WHERE ... is the same as "UPDATE settings SET TheSetting=$title WHERE .... The issue is the OP needs to quote the value -> '$title'
ya i corrected it that was silly mistake made by me. I used double quote instead of single
Closer, but now you need to either remove the dots '.$title.' -> '$title', or add the double quotes back in -> '".$title."'
also, or die(mysql_error()); should be or die(mysqli_error($con)); -> mysqli vs mysql

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.