简体   繁体   中英

PHP, MySQL stored procedure not storing data as expected

There does not seem to be a repeat of this question and I feel this should be a simple fix. I have tested the stored procedure and when adding the parameters it works fine, I have also tested a simple SQL statement which also works as expected. However, when I try to pass my variables into the statement, I get a "Zero results using SQL:" I have also tried to do this as just a prepared statement or as just pure SQL with my variables, but again it never stores anything in the database. The debug output shows what I am trying to pass, but nothing goes into the database

Here is the full code

require_once (getcwd() . "/lib/dataLib.php");
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
    if (isset($_POST['btnSubmit']))
    {
        $projectName = $_POST['projectName'];
        $projectDescription = $_POST['projectDescription'];
        $projectLink = $_POST['projectLink'];
        addItemToDatabase($projectName, $projectDescription, $projectLink);
    }
}


/********************************
* addItemToDatabase
*******************************/
function addItemToDatabase($name, $description, $link)
{
$projectLinkSanSpace = str_replace(' ', '', $link);
$projectLinkAsLink = "<a href='project/" . $projectLinkSanSpace . "'>" . $link . "</a>";

databaseConnection('programWork');
$conn = new mysqli(DBF_SERVER, DBF_USER, DBF_PASSWORD, DBF_NAME);
if ($conn -> connect_error)
{
    die("Connection Failed!: " . $conn ->connect_error);
}
/*$sql = 'insert into projectList (
 *projectName, 
 *projectDescription, 
 *projectPage, 
 *projectSource) 
 *Values ("Stuff", "Things", "Yeah", "Yeah")'; */
$sql = "call insertItemIntoWork($name, $description, $projectLinkAsLink, $projectLinkAsLink)";
$result = $conn->query($sql);
displayResult($result, $sql);
}

some notes on the code, I am using a lib to call external functions which I did not copy/paste here, as I do not think they are relevant to the question. I also did not include the HTML bits which is just the form which should be fairly straight forward, and should work since the debug displayResults() shows values.

Bottom line question is, is there something procedural that I am screwing up here. I do not have to call a function I suppose but is this a situation where the variables are set after the query is ran?

UPDATE I added an error handler per Jay below:

$echoSuccess = true;
$msg = "The query completed successfully";
if ($conn->query($sql) === TRUE)
{
    if ($echoSuccess)
    {
        echo "<span class='errMsg'>" . $msg . " successful.</span><br />";
    }
} else
{
    echo "<strong>Error when: " . $msg . "</strong> using SQL: " . $sql . "<br />" . $conn->error;
    //displayResult($result, $sql)
}

Saying there are errors in my SQL. I think I can work those out and on account of that I do not think this question needs further answering, but rather illustrates the need of error handlers

So, Thank you Jay Blanchard for pointing me in the right direction, turns out the spaces in my input were causing issues entering items into the database. To fix this I had to add quotes around my parameters, though this seems like an odd requirement (perhaps I am missing something) But it works now as expected. Here are the alterations:

error_reporting(E_ALL); ini_set('display_errors', 1);
include "lib/style.php";
require_once (getcwd() . "/lib/coleSterlingLib.php");
//require_once (getcwd() . "/lib/jsFormInteraction.js");
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
    if (isset($_POST['btnSubmit']))
    {
        $projectName = $_POST['projectName'];
        $projectName = "'" . $projectName . "'";
        $projectDescription = $_POST['projectDescription'];
        $projectDescription = "'" . $projectDescription . "'";
        $projectLink = $_POST['projectLink'];
        $projectLink = "'" . $projectLink . "'";
        addItemToDatabase($projectName, $projectDescription, $projectLink);
    }
}

Note the $projectName = "'" . $projectName . "'"; $projectName = "'" . $projectName . "'";

Everything else stayed roughly the same

function addItemToDatabase($name, $description, $link)
{
$projectLinkSanSpace = str_replace(' ', '', $link);
$projectLinkAsLink = "<a href='project/" . $projectLinkSanSpace . "'>" . $link . "</a>";

databaseConnection('programWork');
$conn = new mysqli(DBF_SERVER, DBF_USER, DBF_PASSWORD, DBF_NAME);
if ($conn->connect_error)
{
    die("Connection Failed!: " . $conn->connect_error);
}
//$sql = 'insert into projectList (projectName, projectDescription, projectPage, projectSource) Values ("Stuff", "Things", "Yeah", "Yeah")';
$sql = "call insertItemIntoWork($name, $description, $link, $link)";
//$result = $conn->query($sql);
$echoSuccess = true;
$msg = "The query completed successfully";
if ($conn->query($sql) === TRUE)
{
    if ($echoSuccess)
    {
        echo "<span class='errMsg'>" . $msg . " successful.</span><br />";
    }
} else
{
    echo "<strong>Error when: " . $msg . "</strong> using SQL: " . $sql . "
<br />" . $conn->error;
    //displayResult($result, $sql)
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM