Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

I need help Identifying if I've made a mistake in either html or php code snippet

I'm trying to pass some values to a php file from an html file by using a <form action="addExp.php"> tag. The two variables I want to pass is the "catGory" and the "amount" values.

Everytime It succesfully directs me to the addExp.php page but it doesn't there always seem to be some error displayed in the browser's console:

enter image description here

My html code:

<form action="addExp.php">
    <div style="position: fixed;" id="Cat">
      <select name="catGory" id="catSelect">
        <option hidden disabled selected value=''>Select a Category</option>
        <option value="Restaurant">Restaurant</option>
        <option value="Health">Health</option>
        <option value="Groceries">Groceries</option>
        <option value="Shopping">Shopping</option>
        <option value="Travelling">Travelling</option>
        <option value="Education">Education</option>
        <option value="Work">Work</option>
        <option value="Bills/Taxes">Bills/Taxes</option>
      </select>
    </div>


    <input type="text" onfocus="this.value=''" value="0" step="0.01" id="amount" name="amount">
</form>

My "addExp.php" code:

  <?php
  //phpinfo();  //Uncomment to check php info

  //Declare the variables that will receive the payment infos
  $inAmount = $_POST['amount'];
  $trnCat = $_POST['catGory'];


  //variables that define the connection to the database
  $servername = "localhost";
  $username = "rahim";
  $password = "**********";
  $dBase = "Users";

  // Create connection
  $conn = new mysqli($servername, $username, $password, $dBase);


  // Check connection
  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }

  //Check if the Expense table exists or no
  $val = $conn->prepare('select 1 from `Expenses` LIMIT 1');

  //Create Table query first 
  $sQuer1 = "CREATE TABLE Expenses(
    Amount FLOAT(9,2),
    Category CHAR,
    pyDate Date, 
  )";


  //Execute addition of record into Table
  if ($val !== FALSE) //if the Table exists
  {
    //Prepare statement and parameters to be executed into the query
    $statmnt = $conn->prepare("INSERT INTO Expenses (Amount,Category,pyDate) VALUES (?,?,?)"); //prepare SQL statement/query for execution
    $statmnt->bind_param("dss", $inAmount, $trnCat, date("l d-m")); //bind the selected parameters to the SQL statement   

    //Execute the query statement
    $statmnt -> execute();

    //show that data is added
    echo "Table exists, so data have been added";

  } else {
    //Create the table because table doesn't exist
    $conn->query($sQuer1);  // query() function performs a query against a database

    //Prepare statement and parameters to be executed into the query
    $statmnt = $conn->prepare("INSERT INTO Expenses (Amount,Category,pyDate) VALUES (?,?,?)"); //prepare SQL statement/query for execution
    $statmnt->bind_param("dss", $inAmount, $trnCat, date("l d-m")); //bind the selected parameters to the SQL statement

    //Execute the query statement
    $statmnt -> execute();

    //show that table is created 
    echo "The Table has been creaetd";
  }

  ?>

Edit: I also forgot to show my apache2 error logs: Error logs

question from:https://stackoverflow.com/questions/65932981/i-need-help-identifying-if-ive-made-a-mistake-in-either-html-or-php-code-snippe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I guess you need to set the form method to post to receive the parameters of post parameters

<form method="POST" action="addExp.php">
   <select name="catGory" id="catSelect"> 
    ...

...

set to submitted value (with check if that value actually exists for that key)

$trnCat = array_key_exists('catGory', $_POST) ? $_POST['catGory'] : false;

to see what the error is look at the response tab in inspector, if nothing ist display you could google for php error_reporting / debug log

or you wrap your code into

try {
  //your code
} catch (Exception $e) {
  echo $e->getMessage();
}

and then look at response


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...