-2

i understand the basic if statement as a beginner

if (TRUE){
//Do something
}

i think i have applied it and seems not to be working, i want to simply echo SUCCESSFUL when insert value to table:

$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 enumdata (status) VALUES (%s)",
                       GetSQLValueString($_POST['text'], "text"));

  mysql_select_db($database_RS, $RS);
  $Result1 = mysql_query($insertSQL, $RS);
  if($Result1(TRUE)){
    echo'Successful';  
  }


  $insertGoTo = "insert.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

This returns fatal error.

MYSQL Echo alternatively Echo Insert Successful

if( rowCount() == 1 ) Rows Affected// 
echo successful
  • 1
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). – Jay Blanchard Nov 25 '14 at 15:09
  • A fatal error doesn't necessarily mean your echo won't work, nothing can work when there is a fatal error. Fix that, then move on to the script. What is the fatal error? – James Nov 25 '14 at 15:09
  • [`rowCount()`](http://php.net/manual/en/pdostatement.rowcount.php) is a PDO function. So, why use it with `mysql_query`. Show full code. – Funk Forty Niner Nov 25 '14 at 15:09
  • 1
    @JayBlanchard Actually Jay, OP seems to be using both ;) – Funk Forty Niner Nov 25 '14 at 15:15
  • i didnt use that i read about it some where, though about it been possible for use above... – pmcs management Nov 25 '14 at 15:16
  • So why put it in your question then? > *"This returns fatal error. MYSQL Echo alternatively Echo Insert Successful `if( rowCount() == 1 ) Rows Affected// echo successful`"* - This tells me that's it's part of your working code. Edit your question to reflect the use of `mysql_*` function(s) and not a mix of APIs. – Funk Forty Niner Nov 25 '14 at 15:17
  • [Read the `F` manual](http://php.net/manual/en/function.mysql-error.php) – Funk Forty Niner Nov 25 '14 at 15:25

2 Answers2

0

$Result1(TRUE) is an outright syntax error. $Result1 will be a query HANDLE as returned by mysql_query() upon success or a boolean false to signify failure. Never will it be a function.

$Result1 = mysql_query(...);
if ($Result1 === false) {
   die(mysql_error());
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Did you notice OP's last code block? As per [my comment](http://stackoverflow.com/questions/27130144/successful-message-either-by-inserted-values-or-rows-affected#comment42759366_27130144) – Funk Forty Niner Nov 25 '14 at 15:12
  • it works amazingly but it doesn't return to location header after insert and echoes great.. WHY? $insertGoTo = "insert.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } – pmcs management Nov 25 '14 at 15:36
  • @MarcB how do i check if headers already sent cause if i post it echos result and gets stucked with echoed result, i want to add location header that is, after result echoed return to location header.. – pmcs management Nov 25 '14 at 22:18
  • header() returns false if it fails. – Marc B Nov 26 '14 at 14:11
0

You can use like

$result = mysql_query($sql);

if($result)
   Success
else 
   error
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
jay.jivani
  • 1,560
  • 1
  • 16
  • 33