简体   繁体   中英

Post some data to mysql database in specific fields through window.prompt (code isn't running properly)

I am trying to post some data to the server, which is running MySQL. I am running the following code but the console doesn't show me any error. Could you please take a look and tell me if you see something wrong? Any help would be appreciate because I can't find similar guidelines around..

What I want my code to do is this: I want the user, through the window.prompt, to give 4 values which will be stored in the variables: user_id, book_id, game_id, site_id. Then these 4 values must be stored in my database...

index.html

    <html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script>
    function save3()
    {
    $("#user_id").val(prompt("Give the UserId:"))
    $("#book_id").val(prompt("Give the BookId:"))
    $("#game_id").val(prompt("Give the GameId:"))
    $("#site_id").val(prompt("Give the SiteId:"))
    }
   </script>

</head>
  <body>

<p align="center">example</p>
<table align="center" width="730">
<tr>
<td align="center">
<div>
  <table class="blueTable" style="float: left">
    <thead>
<tr>
 <th colspan="1"><u>Menu</u></th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="button" value="New" id="new" onclick="new1()" class="button12"/></td></tr>
<tr>
<td><input type="button" value="Load" id="load" onclick="load2()" class="button12"/></td></tr>
<tr>
<td><form name="SaveGame" id="SaveGame" method="post" action="http://127.0.0.1/PHP/mine2.php" enctype="multipart/form-data">
<input type="submit" value="Save" id="save" onclick="save3()" class="button12"/>
<input type="hidden" name="user_id" id="user_id" >
<input type="hidden" name="book_id" id="book_id" >
<input type="hidden" name="game_id" id="game_id" >
<input type="hidden" name="site_id" id="site_id" >
</form>
 <script>
        $("#SaveGame").submit(function(e) {


    var form = $(this);
    var url = form.attr('action');

        $.ajax({
               type: "POST",
               url: url,
               data: form.serialize(), // serializes the form's elements.
               success: function(data)
               {
                   alert("The game has been saved!"); // show response from the php script.
               }
             });

        e.preventDefault(); // avoid to execute the actual submit of the form.
    });
    </script>
</body>
</html>

mine2.php

    <?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("127.0.0.1", "root", "", "mysql3");
// Check connection
if($link === false) {
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$user_id =$_POST['user_id'];
$book_id =$_POST['book_id'];
$game_id =$_POST['game_id'];
$site_id =$_POST['site_id'];

if (mysql_query("INSERT INTO `components` (`user_id`, `book_id`, `game_id`, `site_id`) VALUES ('".$user_id."','".$book_id."','".$game_id."', '".$site_id."',)"))

// Attempt insert query execution
//$sql = "INSERT INTO components (user_id, book_id, game_id, site_id) VALUES ('6', '6', '6', '6')";
//if(mysqli_query($link, $sql)){
//} else{
//}

// Close connection
mysqli_close($link);
?>

Guys, also I found this question: JS Prompt to PHP Variable Should I use something from there?

You need to add the information you are getting from promtps into the form. The best way is using type hidden inputs:

<input type="hidden" name="user_id" id="user_id" >

and into your save3() function

$("#user_id").val(prompt("Give the UserId:"))

Same way for the other variables.

Also, you need to verify your PHP code. I saw few errors. Try using this:

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = new mysqli("127.0.0.1", "root", "", "mysql3");

// Check connection
if($link->connect_errno) {
   die("ERROR: Could not connect. " . $link->connect_error);
}

$user_id =$_POST['user_id'];
$book_id =$_POST['book_id'];
$game_id =$_POST['game_id'];
$site_id =$_POST['site_id'];

if ($result = $link->query("INSERT INTO `components` (`user_id`, `book_id`, `game_id`, `site_id`) VALUES ('$user_id','$book_id','$game_id', '$site_id')") == true)
  echo json_encode($result);
else {
  echo "Error";
}

// Attempt insert query execution
//$sql = "INSERT INTO components (user_id, book_id, game_id, site_id) VALUES ('6', '6', '6', '6')";
//if(mysqli_query($link, $sql)){
//} else{
//}

// Close connection
$link->close();
?>

The method mysqli_connect and mysql_query were deprecated, I propose to use new mysqli() instead.

And check you INSERT statement, you have a syntax error.

Let me know if it works for you please.

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