B4J Question Inserting data to online mysql database

saeed10051

Active Member
Licensed User
Longtime User
i am trying to insert data to my online database using following php script
B4X:
<?php
  $conn = new mysqli("fdb23.awardspace.net", "3373050_restaurant", "abc123", "3373050_restaurant");
 
  if ($conn->connect_error) {
    die("ERROR: Unable to connect: " . $conn->connect_error);
  }

  echo 'Connected to the database.<br>';

$id = $_REQUEST('id');
$name = $_REQUEST('name');
$price = $_REQUEST('price');


$query = $conn->stmt_init() ;

if ( $query->prepare("INSERT INTO menuitems SET itemid = ?, itemname = ?, itemprice = ?") {
  $query->bind_param('isi',$_REQUEST['id'],$_REQUEST['name'],$_REQUEST['price']) ;
  $query->execute() ;
}

if($conn->query($sql)===TRUE){
    echo "SUCCESS!";
}else{
    echo "ERROR"
}

$conn->close();
?>
i am getting following error in log
<b>Parse error</b>: syntax error, unexpected ';' in <b>/srv/disk2/3373050/www/saeedhassan.atwebpages.com/i.php</b> on line <b>20</b><br />
The B4J code that i am running to insert data is following
B4X:
Dim id As Int
    Dim name As String
    Dim price As Int
    id = Txtid.Text
    name = Txtname.Text
    price = Txtprice.Text
    
    Dim j As HttpJob
    j.Initialize("INSERT", Me)
    j.Download2("http://saeedhassan.atwebpages.com/i.php", Array As String ("id", id, "name", name, "price", price))
    Wait For (j) jobdone (j As HttpJob)
    If j.Success = True Then
        Log(j.GetString)
    Else
        Log("Error: " & j.ErrorMessage)
        
    End If
    j.Release
 

saeed10051

Active Member
Licensed User
Longtime User
the error as per php numbering is coming on following line
$query->bind_param('isi',$_REQUEST['id'],$_REQUEST['name'],$_REQUEST['price']) ;
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You are not using it correctly i guess. bind_param needs variables.

PHP:
<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');

/* prüfe Verbindung */
if (mysqli_connect_errno()) {
    printf("Verbindung fehlgeschlagen: %s\n", mysqli_connect_error());
    exit();
}

$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
$stmt->bind_param('sssd', $code, $language, $official, $percent);

$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;

/* führe Anweisung aus */
$stmt->execute();

printf("%d Zeile eingefügt.\n", $stmt->affected_rows);

/* schließe Anweisung */
$stmt->close();
 
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
Thanks DonManfred. But when i use following php code
B4X:
<?php
  $conn = new mysqli("pdb48.awardspace.net", "3373050_restaurant", "abc123", "3373050_restaurant");
 
  if ($conn->connect_error) {
    die("ERROR: Unable to connect: " . $conn->connect_error);
  }

  echo 'Connected to the database.<br>';

$id = $_REQUEST('id');
$name = $_REQUEST('name');
$price = $_REQUEST('price');

$query = $conn->prepare("INSERT INTO menuitems (itemid, itemname, itemprice) VALUES (?, ?, ?)");
$query->bind_param("sss", $id, $name, $price);

//$query = $conn->stmt_init() ;

//if ( $query->prepare("INSERT INTO menuitems SET itemid = ?, itemname = ?, itemprice = ?") {
//  $query->bind_param('sss',$_REQUEST['id'],$_REQUEST['name'],$_REQUEST['price']) ;
//  $query->execute() ;
//}

if($conn->query($sql)===TRUE){
    echo "SUCCESS!";
}else{
    echo "ERROR";
}

$conn->close();
?>
i am getting following fatal error in b4j log


Connected to the database.<br><br />
<b>Fatal error</b>: Uncaught Error: Function name must be a string in /srv/disk2/3373050/www/saeedhassan.atwebpages.com/i.php:10
Stack trace:
#0 {main}
thrown in <b>/srv/disk2/3373050/www/saeedhassan.atwebpages.com/i.php</b> on line <b>10</b><br />

line 10 in the php code is
$id = $_REQUEST('id');
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Line 17 missing one parentheses
i am trying to insert data to my online database using following php script
B4X:
<?php
  $conn = new mysqli("fdb23.awardspace.net", "3373050_restaurant", "abc123", "3373050_restaurant");

  if ($conn->connect_error) {
    die("ERROR: Unable to connect: " . $conn->connect_error);
  }

  echo 'Connected to the database.<br>';

$id = $_REQUEST('id');
$name = $_REQUEST('name');
$price = $_REQUEST('price');


$query = $conn->stmt_init() ;

if ( $query->prepare("INSERT INTO menuitems SET itemid = ?, itemname = ?, itemprice = ?") {
  $query->bind_param('isi',$_REQUEST['id'],$_REQUEST['name'],$_REQUEST['price']) ;
  $query->execute() ;
}

if($conn->query($sql)===TRUE){
    echo "SUCCESS!";
}else{
    echo "ERROR"
}

$conn->close();
?>
i am getting following error in log
<b>Parse error</b>: syntax error, unexpected ';' in <b>/srv/disk2/3373050/www/saeedhassan.atwebpages.com/i.php</b> on line <b>20</b><br />
The B4J code that i am running to insert data is following
B4X:
Dim id As Int
    Dim name As String
    Dim price As Int
    id = Txtid.Text
    name = Txtname.Text
    price = Txtprice.Text
   
    Dim j As HttpJob
    j.Initialize("INSERT", Me)
    j.Download2("http://saeedhassan.atwebpages.com/i.php", Array As String ("id", id, "name", name, "price", price))
    Wait For (j) jobdone (j As HttpJob)
    If j.Success = True Then
        Log(j.GetString)
    Else
        Log("Error: " & j.ErrorMessage)
       
    End If
    j.Release
 
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
there is no such method id, name or price

PHP:
$price = $_REQUEST('price'); // WRONG!
$price = $_REQUEST['price']; // Correct
Thanks Boss
but now i am getting this new error
I have updated the php code as follows
B4X:
$id = $_REQUEST['id'];
$name = $_REQUEST['name'];
$price = $_REQUEST['price'];

$query = $conn->prepare("INSERT INTO menuitems (itemid, itemname, itemprice) VALUES (?, ?, ?)");
$query->bind_param("sss", $id, $name, $price);


//$query = $conn->stmt_init() ;


//if ( $query->prepare("INSERT INTO menuitems SET itemid = ?, itemname = ?, itemprice = ?") {
//  $query->bind_param("sss", $_REQUEST['id'], $_REQUEST['name'], $_REQUEST['price']) ;
//  $query->execute() ;
//}

if($conn->query($sql)===TRUE){
    echo "SUCCESS!";
}else{
    echo "ERROR";
}

$conn->close();

i am getting following error
<b>Warning</b>: mysqli::query(): Empty query in <b>/srv/disk2/3373050/www/saeedhassan.atwebpages.com/i.php</b> on line <b>26</b><br />

line 26 is this one
if($conn->query($sql)===TRUE){
 
Upvote 0
Top