Jump to content

Recommended Posts

I completed a whole website using Dreamweaver and Xampp,  all local tested ... so happy :).

 

I now transition the Databases and everything via FTP. I have tested the connections with this code:

This page is located in the  localhost/Connections/security.php

<?php
$hostname_nglmain = "hostname here";
$database_nglmain = "DB Name";
$username_nglmain = "username here";
$password_nglmain = "my password!";
$nglmain = mysqli_connect($hostname_nglmain ,$username_nglmain,$password_nglmain,$database_nglmain);
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
  echo "we are good";	
}
?>

and i get We are good when i call the test insertions page i created

<?php require_once('Connections/security.php'); ?>


<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO testtable (id, `data`) VALUES (%s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString($_POST['data'], "text"));

  mysql_select_db($database_nglmain, $nglmain);
  $Result1 = mysql_query($insertSQL, $nglmain) or die(mysql_error());
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Id:</td>
      <td><input type="text" name="id" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Data:</td>
      <td><input type="text" name="data" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"> </td>
      <td><input type="submit" value="Insert record" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p> </p>
</body>
</html>


I can not get the records to insert .. am i missing something during the transfer ... I am sure it had to be a coma or a period somewhere .... but I need help   :(

 

 

When i hit insert i get a we are good, page goes blank

 

 

Thx in advance

 

 

Link to comment
https://forums.phpfreaks.com/topic/291418-because-i-am-dummb-simple-code-help/
Share on other sites

Why do you check if a function exists that you are only going to create?  Are you duplicating some already installed function name?

 

As for your real problem - you do a connect using mysqli but then you try and use MySQL functions which are deprecated.

The function.. well that is generated by Dreamweaver. I was trying to avoid typing to much code but now i am paying the price :(

 

 

 

 

Second question you are saying that where everI used mysql I should change to mysqli.

 

Sample:

  • mysql_select_db($database_nglmain, $nglmain);
  • $Result1 = mysql_query($insertSQL, $nglmain) or die(mysql_error());

 

Should be :

 

  • mysqli_select_db($database_nglmain, $nglmain);
  • $Result1 = mysqli_query($insertSQL, $nglmain) or die(mysqli_error());

The order of the function parameters also changes (refer to the manual) and the connection parameter is not optional as it is with mysql_xxx functions . Also not all functions have mysqli equivalent and mysqli has new ones.

I change the code ... remove the useless function.. now i get an error on line 12 ...

 

GetSQLValueString($_POST['id'], "int"),

 

So frustarting

<?php require_once('Connections/nglmain.php'); ?>


<?php
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO testtable (id, 'data') VALUES (%s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString($_POST['data'], "text"));

  mysqli_select_db($database_nglmain, $nglmain);
  $Result1 = mysqli_query($insertSQL, $nglmain) or die(mysqli_error());
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Id:</td>
      <td><input type="text" name="id" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Data:</td>
      <td><input type="text" name="data" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"> </td>
      <td><input type="submit" value="Insert record" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p> </p>
</body>
</html>


......

Should be :

 

  • mysqli_select_db($database_nglmain, $nglmain);

 

 

you are selecting the database in the mysqli_connect() statement. there's no need to re-select it, which dreamweaver does before every query statement. in fact, forget most of what DreamWeaver does and just learn to write your own code.

This is use to be a friendsly site  :( 

 

mac_gyver your a bit rude dude

 

I came here because,  this site has always been suportive and  helpful. It my questions bother you i apologized just don't awnser. Thx for the effort

 

Still not working, maybe is a larger issue than the code.

 

~J

if i had posted that YOUR code is crap, that would have been rude. that's not what anyone stated.

 

likewise, no one stated to remove the GetSQLValueString() function. it was questioned why it was being conditionally defined and if you are duplicating an existing function by having it in the code. that was a question about the code, not a statement to do anything to the code.

 

you would in fact still need that function, which is what the fatal undefined function .... error message you saw means, unless you plan on writing your own code to correctly handle each data value being put into sql query statements (or use prepared queries.) if you keep using it, you will need to properly convert it to use mysqli_real_escape_string() and pass the mysqli connection resource into the function. if you don't plan to keep using it, in addition to correctly handling each data value being put into the sql query statements, you will also need to modify the sql syntax slightly (or use prepared queries), because that function supplies some of the quoting around literal string values, rather than it being in the actual query statement.

 

lastly, mysqli_error(....)  requires the mysqli connection resource as a parameter, otherwise you will just get php errors for mysql_error() statement, rather than any database error message.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.