Ari G
Ari G

Reputation: 177

Why will it not update to the database?

This is my code :

<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="blah"; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="SubCategories"; // Table name 

$con=mysqli_connect("$host", "$username", "$password", "$db_name");

if (mysqli_connect_errno()) // Check connection
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" /> 
        <title></title>
    </head>
    <body>
    <form action="untitled.php" method="post"><!-- untitled.php -->

<?php
  //print_r($_POST); //print all checked elements
  //echo "<br>".$email, $_POST["update"][$i];
  //mysql_real_escape_string ($route )

if(isset($_POST['submit'])) {
    foreach ($_POST["holder"] as $i=>$email) { 
        $y=$email;
        $h=$_POST["update"][$i];
        $res2=mysqli_query("UPDATE ".$tbl_name." SET subCat2 = '" . $y . "' WHERE id =". $h,$con);
        if ($res2){

        }
        else{
            echo "<h1>NOT WORKING!</h1>";
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }       
    }

}

$result = mysqli_query($con,"SELECT * FROM $tbl_name");

echo "<br>";
while($row = mysqli_fetch_array($result))
  {
    echo '<input type="text" name="holder[]" id="checkbox-1" class="custom" value=" ' . $row['subCat2'] . '"/>';
    echo '<input type="hidden" name="update[]" id="checkbox-1" class="custom" value=" ' . $row['subCatNum'] . '"/>';
    echo "<br>";
  }
?>
</br>
        <input type="submit" name="submit">
    </form>

    </body>
</html>

I can't update the table in my database. I am able to extract the variables properly and echo them, however it does not work.

I have gotten the following error in the past 'no database selected'.

Upvotes: 0

Views: 113

Answers (4)

Ari G
Ari G

Reputation: 177

Someone in my class helped me figure it out, thanks though! Here is the code, just wonderful :)

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="blah"; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="test_mysql"; // Table name 

$con=mysqli_connect($host,$username,$password,$db_name);

if (mysqli_connect_errno()) // Check connection
  {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


?>

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" /> 
        <title></title>
    </head>
    <body>
    <form action="untitled.php" method="post"><!-- untitled.php -->

<?php

if(isset($_POST['submit'])) {
    foreach ($_POST["holder"] as $i=>$email) { 
        $y=$email;
        $h=$_POST["update"][$i];
        $sql2="UPDATE ".$tbl_name." SET name = '" . $y . "' WHERE id =". $h;
        //$res2=mysqli_query("UPDATE ".$tbl_name." SET name = '" . $y . "' WHERE id =". $h,$con);
        $res2=mysqli_query($con,$sql2);
        if ($res2){

        }
        else{
            echo "<h1>NOPE!</h1>";
            print "Failed to connect to MySQL: " . mysqli_error();

        }       
    }

}



$result = mysqli_query($con,"SELECT * FROM ".$tbl_name);



echo "<br>";
while($row = mysqli_fetch_array($result))
  {
    echo '<input type="text" name="holder[]" id="checkbox-1" class="custom" value=" ' . $row['name'] . '"/>';
    echo '<input type="hidden" name="update[]" id="checkbox-1" class="custom" value=" ' . $row['id'] . '"/>';
    //echo '<input type="text" class="a" name="holder2[]" id="checkbox-1" class="custom" value="' . $row['price'] . '" />';
    echo "<br>";
  }


?>

        </br>
        <input type="submit" name="submit">
    </form>

    </body>
</html>

Upvotes: 0

Hugo B.
Hugo B.

Reputation: 503

I think that you forgot to select the database. Try to put this after your connection:

if (!mysqli_select_db($con, $db_name)) {
    die("Uh oh, couldn't select database $db_name");
}

If this happens, double check the name, permissions, etc.

Upvotes: 1

medina
medina

Reputation: 8159

You should change your code adding the snippet below. This way you can debug your code better:

if (!$result = $mysqli->query("YOUR-SQL", MYSQLI_USE_RESULT)) {
     printf("Error: %s\n", $mysqli->error);
}
...do something here..

$result->close();

Upvotes: 0

Giacomo1968
Giacomo1968

Reputation: 26066

Try it again, but without the quotes surrounding the DB connection variables. I mean, they are variables & not strings, right?

Original with quotes:

$con=mysqli_connect("$host","$username","$password","$db_name");

Cleaned without quotes:

$con=mysqli_connect($host,$username,$password,$db_name);

Upvotes: 0

Related Questions