PHP INSERT en la base de datos de alojamiento

I have this PHP code that inserts id, firstname and lastname:

<?php
header('Access-Control-Allow-Origin: *');

$conn = mysql_connect('localhost', 'happy', '***');
$db   = mysql_select_db('dbtest');


$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

mysql_query("INSERT INTO tbltry (firstname, lastname) VALUES('$firstname', '$lastname')");
?>

And I have this HTML with AJAX:

<script>
function save(){
var xmlhttp;

         if (window.XMLHttpRequest)
         {
            xmlhttp=new XMLHttpRequest();
         }
         else
         {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
         }


        xmlhttp.open("POST", " **url** ", true);
        console.log("added");
        xmlhttp.send();

        xmlhttp.onreadystatechange=function()
         {
         if (xmlhttp.readyState==4 && xmlhttp.status==200)
             {

             }
         }
}
</script>
</head>
<body>

Firstname: <input type="text" name="firstname" id="firstname">
Lastname: <input type="text" name="lastname" id="lastname">
<button onclick="save()">Save</button>
</body>

This two are working fine, it insert a new row in database, but the only data that is inserted is the id on id column and column firstname and lastname is blank.

Can someone tell what am I missing? Any help would be appreciated.

preguntado el 27 de noviembre de 13 a las 05:11

can you correct this spelling $fisrtname to $firstname -

Side note: Your PHP script has an open invitation for SQL injection attacks. -

also please try shifting from mysql* extensions to PDO as mysql* extensions are deprecated officially -

su función javascript save needs to know what data to send (your inputs) on the post request. -

It could be the typo in your code; you had VALUES('$fisrtname', '$lastname') en lugar de VALUES('$firstname', '$lastname') . See if that fixes it. -

2 Respuestas

Prueba esta

<script>
function save(){
var xmlhttp;

var fname = document.getElementById("firstname").value;
 var lname = document.getElementById("lastname").value;

var data = {firstname:fname,lastname:lname};
         if (window.XMLHttpRequest)
         {
            xmlhttp=new XMLHttpRequest();
         }
         else
         {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
         }


        xmlhttp.open("POST", " **url** ", true);
        console.log("added");
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xmlhttp.send(data);

        xmlhttp.onreadystatechange=function()
         {
         if (xmlhttp.readyState==4 && xmlhttp.status==200)
             {

             }
         }
}

respondido 27 nov., 13:05

Tried this one but still blank. - Cyan Pile

Prueba algo como esto

function save(){
 var fname = document.getElementById("firstname").value;
 var lname = document.getElementById("lastname").value;
 var data = "firstname="+fname+"&lastname="+lname;  
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {

            alert(xmlhttp.responseText);

        }
      }

    xmlhttp.open("POST","url?"+data,true);
    xmlhttp.send();

}

your test file should be like

<?php

$conn = mysql_connect('localhost', 'happy', '***');
$db   = mysql_select_db('dbtest');


$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

mysql_query("INSERT INTO tbltry (firstname, lastname) VALUES('$firstname', '$lastname')");
?>

Instead of using old xmlhttp its better to use jQuery.Ajax, Also mysql functions are deprecated use PDO.

You should use any mysql escape function for security.

Espero eso ayude..

respondido 27 nov., 13:07

I tried this one but firstname and lastname is still blank and no alert. I'm not sure if I did it right like this: xmlhttp.open("GET","http://url.com/dir/testing.php?"+data,true); Y este de aquí: var data = "firstname="+fname+"&lastname="+lname; because the lastname has '&' and firstname don't have. - Cyan Pile

its query string param then entire url will be like url.com/dir/testing.php?firstname=your name&lastname=your name , second one required & check my updated code your firstname in query has a typo(spell issue). - Jobín

Try my code exactly. alert() dont have any data bcoz your nothing output from that page. - Jobín

It's still not inserting firstname and lastname only id. I'll try something more and will get back here. Thank you so much my friend for the help. I really appreciate it. - Cyan Pile

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.