Jump to content

MichelDS

Members
  • Posts

    29
  • Joined

  • Last visited

    Never

Everything posted by MichelDS

  1. I don't want to be disrespectful but it wasnt meant to check the codes but more about the theory. However I solved it by redoing some variables even when there was no error and by using sleep(0.5). I encountered the same problem with ASP in the past and when I was searching the net yesterday, I see that I am not the only one ! Thanks anyway ! Hey, I can't change the status to RESOLVED !
  2. I understand what you're saying. There's no header before the update script involved ! Only at the end of the update and it has the exit; as well. The joke is that when I put die(); after the update, everything works well ! Can there be a problem with the speed and variables that's needed between the two queries ? I have already looked at the codes before posting this and it's not the codes, nor the script , this I am 100% sure. That's why I am thinking in an other directon than the codes. But, am I using too much variables ? codes between the 2 queries : $arrayLordervalue = explode(",", $orderlink); $diff = array_diff($arrayLordervalue,$arraydel); $orderlinknewvalue = orderlink($diff); $orderlinknewvalue = mysql_real_escape_string($orderlinknewvalue); $linksubid = mysql_real_escape_string($linksubid); just for info : function ordervalue is for reading vars from an array : // read vars uit array function orderlink($somevalue){ if(!empty($somevalue)){ $i = 1; $values =""; $value2 =""; foreach($somevalue as $value) { if($i == 1){ $values[$i] = $value;} else { $values[$i] = ",". $value; } $value2= $value2.$values[$i]; $i++; } return $value2; } }
  3. I got a strange question... I have wamp installed on my PC and have some troubles with the SPEED of executing a script. Not that it is slow, not at all. Only the tests I've done tells me that the speed of the script that is executed can give a wrong result. Is it mysql database that can't handle the speed with wamp on a personal PC ? Thus NOT on a server. I have a script that delete some rows, this works well -> InsertDelUpdateDB($mysqli, $sql, $parameters); than I have to update the data in a certain row, a certain field... When I do a test with echo $var... it gives me the right data. So no fault there. when the script has to update that certain field it doesn't do the job well ! The data is not updated with the right data !!! But it IS updated with a part of the data ! Once again the script has no errors and SHOULD update the field with the right data. So, in short, all the data needed has no errors, it's only the update that makes mistakes when it has to execute 2 queries one after the after (without closing the DB connection), One time it works fine, than it don't... When I do a test and use ONLY the update script with the data needed put in variabales, it works fine ! My question is : can there be a problem with the speed that mysql, wamp has to endure ?
  4. okay, I solved it myself So I post the final code here for the use of Mysqli and the use of a function to retrieve the data or to insert/update or delete the data. Thanks to scootstah ! He had solved the most difficult part. This script uses "?" in the sql query instead of the fieldnames from the table in the database. This should be more secure concerning SQL injection. Also the script don't need you to say if the field is an integer of a string or something else. Normally you will find something as : $stmt->bind_param("si", $firstname, $id); Here there are two fieldnames, the first is a string (firstname) and is written as "s", the second is an integer (id) and is written as "i". This is not necessary anymore, only the fieldnames are required ! The function does the rest. include_once('inc/dsn_start.php'); // Open DB <?php $db_host = "localhost"; $db_gebruiker = "your_username"; $db_wachtwoord = 'your_password'; $db_naam = "your_database_name"; $mysqli = mysqli_connect($db_host, $db_user, $db_pass, $db_name); if ($mysqli->connect_error) { echo ('Could not establish database connection'); // or the "die" code // or something else... } ?> 2 functions : SelectFromDB() for the SELECT sql and InsertDelUpdateDB() for the INSERT, UPDATE and DELETE sql query. <?php function SelectFromDB($mysqli, $sql, $parameters){ //just check if it is the right SQL query "SELECT ..." $phrase = explode (" ", $sql); if(in_array('SELECT',$phrase) OR in_array('select',$phrase)) { if ($stmt = $mysqli->prepare($sql)) { $parameters_ref = array(); foreach(array_keys($parameters) as $key) { $parameters_ref[$key] = &$parameters[$key]; } // loop through the parameters to figure out the types $types = ''; foreach($parameters as $parameter) { if (is_int($parameter)) { $types .= 'i'; } else if (is_float($parameter)) { $types .= 'd'; } else { $types .= 's'; } } // end for each // add the types to the beginning of the parameters array array_unshift($parameters, $types); call_user_func_array(array($stmt, 'bind_param'), $parameters); $stmt->execute(); $result = $stmt->get_result(); $stmt->close(); return $result; } // end if else { return "No results to display !"; } } } // end function //INSERT, UPDATE, DELETE function InsertDelUpdateDB($mysqli, $sql, $parameters){ if ($stmt = $mysqli->prepare($sql)) { $parameters_ref = array(); foreach(array_keys($parameters) as $key) { $parameters_ref[$key] = &$parameters[$key]; } // loop through the parameters to figure out the types $types = ''; foreach($parameters as $parameter) { if (is_int($parameter)) { $types .= 'i'; } else if (is_float($parameter)) { $types .= 'd'; } else { $types .= 's'; } } // add the types to the beginning of the parameters array array_unshift($parameters, $types); call_user_func_array(array($stmt, 'bind_param'), $parameters); $stmt->execute(); return $mysqli->insert_id; $stmt->close(); } } //end function ?> Here the codes with the SQL statement, you choose what you need <?php # SELECT $sql = "SELECT * FROM tbl_link WHERE userid = ?"; $parameters = array($userid); $result = SelectFromDB($mysqli, $sql, $parameters); # INSERT $sql = "INSERT tbl_link(userid, linkcat, linksubid, linklang, linkactive, linktitle) VALUES(?, ?, ?, ?, ?, ?)"; $parameters = array($userid, $one, $null, $lang, $one, $newLink); $LastRowId = InsertDelUpdateDB($mysqli, $sql, $parameters); // you can retrieve the rowid where the data have been inserted // echo $LastRowId; # UPDATE $linktitle = "new test 3,2,1,"; $sql = "UPDATE tbl_link SET linktitle = ? WHERE linkid = ?"; $parameters = array($linktitle, $linkid); InsertDelUpdateDB($mysqli, $sql, $parameters); # DELETE $sql = "DELETE FROM tbl_link WHERE linkid = ?"; InsertDelUpdateDB($mysqli, $sql, $parameters); ?> If you used a "SELECT" sql query, now we will display the data <?php if (!$result) { echo "No valid data !"; } else { // display records if there are records to display if ($result->num_rows > 0) { while ($row = $result->fetch_object()) { // set up a row for each record echo $row->linkid." - ".$row->linksuborder." - ".$row->linktitle."<br />"; } } // if there are no records in the database, display an alert message else { echo "No results to display !"; } } ?> Have fun with it...
  5. you may congratulate yourself scootstah I mean !
  6. Ok, this is what I have and it works ! You may congratulate yourself ! error_reporting(E_ALL | E_STRICT); ini_set('display_errors',1); // 1 == on , 0 == off # sql debug define('DEBUG_MODE',true); // true == on, false == off include_once('../inc/dsn_start.php'); // Open DB function ShowSaveDelUpdateDB($mysqli, $sql, $parameters){ //INSERT, UPDATE, DELETE if ($stmt = $mysqli->prepare($sql)) { $parameters_ref = array(); foreach(array_keys($parameters) as $key) { $parameters_ref[$key] = &$parameters[$key]; } // loop through the parameters to figure out the types $types = ''; foreach($parameters as $parameter) { if (is_int($parameter)) { $types .= 'i'; } else if (is_float($parameter)) { $types .= 'd'; } else { $types .= 's'; } } // add the types to the beginning of the parameters array array_unshift($parameters, $types); call_user_func_array(array($stmt, 'bind_param'), $parameters); $stmt->execute(); return $mysqli->insert_id; $stmt->close(); } } //end function # INSERT $sql = "INSERT tbl_link(userid, linkcat, linksubid, linklang, linkactive, linktitle) VALUES(?, ?, ?, ?, ?, ?)"; $parameters = array($userid, $one, $null, $lang, $one, $newLink); $LastRowId = ShowSaveDelUpdateDB($mysqli, $sql, $parameters); # UPDATE //$sql = "UPDATE tbl_link SET linktitle = ? WHERE linkid = ?"; //$parameters = array($linktitle, $linkid); //ShowSaveDelUpdateDB($mysqli, $sql, $parameters); # DELETE //$sql = "DELETE FROM tbl_link WHERE linkid = ?"; //$parameters = array($linkid); //ShowSaveDelUpdateDB($mysqli, $sql, $parameters); Okay now, let's makes it compleet and do the INSERT part. I got this ready (only INSERT in tin this one so it'll be not confusing, I'll put it together with the rest of the original function later. I got message "return "No results to display !";" + the error when writing out the result of the query : Notice: Trying to get property of non-object -> code : if ($result->num_rows > 0) function ShowSaveDelUpdateDB($mysqli, $sql, $parameters){ // to detect if there's "SELECT ..." in the query : $phrase = explode (" ", $sql); if(in_array('SELECT',$phrase) OR in_array('select',$phrase)) { //same code as usual : if ($stmt = $mysqli->prepare($sql)) { $parameters_ref = array(); foreach(array_keys($parameters) as $key) { $parameters_ref[$key] = &$parameters[$key]; } // loop through the parameters to figure out the types $types = ''; foreach($parameters as $parameter) { if (is_int($parameter)) { $types .= 'i'; } else if (is_float($parameter)) { $types .= 'd'; } else { $types .= 's'; } } // end for each // add the types to the beginning of the parameters array array_unshift($parameters, $types); call_user_func_array(array($stmt, 'bind_param'), $parameters); $stmt->execute(); // new part : $meta = $stmt->result_metadata(); while ($field = $meta->fetch_field()) { $param[] = &$row[$field->name]; } call_user_func_array(array($stmt, 'bind_result'), $param); while ($stmt->fetch()) { foreach($row as $key => $val) { $x[$key] = $val; } $results[] = $x; } $stmt->close(); return $results; } // end if else { return "No results to display !"; } } // end IF SELECT... } // close function // Here we're asking to the function to give us a result # SELECT $sql = "SELECT * FROM tbl_link WHERE userid = ?"; $parameters = array($userid); $result = ShowSaveDelUpdateDB($mysqli, $sql, $parameters); // Code to SHOW the records from the database if (!$result) { echo "No valide data !"; } else { // display records if there are records to display if ($result->num_rows > 0) { echo "<table border='1' cellpadding='10'>"; echo "<tr><th>LinkID</th><th>Linksuborder</th><th>Linktitle</th></tr>"; while ($row = $result->fetch_object()) { // set up a row for each record echo "<tr>"; echo "<td>" . $row->linkid . "</td>"; echo "<td>" . $row->linksuborder . "</td>"; echo "<td>" . $row->linktitle . "</td>"; echo "</tr>"; } echo "</table>"; } // if there are no records in the database, display an alert message else { echo "No results to display !"; } }
  7. It continues... I changed to become an array : $parameters = array("$userid , $one, $null, $lang , $one, $newLink"); Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given -> code : call_user_func_array(array($stmt, 'bind_param'), $parameters); If it should work at the end I'll post the final solution here too !
  8. You don't give up isn't ? You talk about the function that I could not letwork ! So I've posted the code. Just for inserting data. That's my test but It gives me a some errors. (see below) I am in the beginning of a huge project , so it's important that I decide from the beginning which code to use and how from getting/inserting/... data from/into the database. Only this thing with mysqli is not so simple if you want to make a function with it apparently ! <?php // variables that normally comes from somewhere else $userid = "11"; $lang ="1"; $newLink = "testing12345"; $one = 1; $null = 0; include_once('includes/dsn_start.php'); // Opening the datase just as you said. function ShowSaveDelUpdateDB($mysqli, $sql, $parameters){ if ($stmt = $mysqli->prepare($sql)) { //$stmt->bind_param($parameters); // loop through the parameters to figure out the types $types = ''; foreach($parameters as $parameter) { if (is_int($parameter)) { $types .= 'i'; } else if (is_float($parameter)) { $types .= 'd'; } else { $types .= 's'; } } // add the types to the beginning of the parameters array array_unshift($parameters, $types); call_user_func_array(array($stmt, 'bind_param'), $parameters); $stmt->execute(); //return $mysqli->insert_id; $stmt->close(); } } //end function // Here I'm asking the function to insert $sql = "INSERT tbl_link(userid, linkcat, linksubid, linklang, linkactive, linktitle) VALUES(?, ?, ?, ?, ?, ?)"; $parameters = "$userid , $one, $null, $lang , $one, $newLink"; ShowSaveDelUpdateDB($mysqli, $sql, $parameters) ; ?> Warning: Invalid argument supplied for foreach() -> code : foreach($parameters as $parameter) Warning: array_unshift() expects parameter 1 to be array, string given -> code : array_unshift($parameters, $types); Warning: call_user_func_array() expects parameter 2 to be array, string given -> code : call_user_func_array(array($stmt, 'bind_param'), $parameters);
  9. I didn't succeeded to make this work. Thus with regret I have to do it in a very stupid and extra long way $mysqli = opendb($db_host, $db_user, $db_pass, $db_name); if(!$mysqli){ echo "An error has occured, no data available"; } else { $sql= "INSERT tbl_link(userid, linkcat, linksubid, linklang, linkactive, linktitle) VALUES(?, ?, ?, ?, ?, ?)"; if ($stmt = $mysqli->prepare($sql)) { $stmt->bind_param("iiiiis", $userid , $one, $null, $lang , $one, $newLink); $stmt->execute(); $newlinkid = mysqli_insert_id($mysqli); //$mysqli_stmt->insert_id(); // I need the inserted last row ID $stmt->close(); } $mysqli->close(); The only function is opendb(...) with the variables init, this way I don't need to use the global statement. But how long like a prehistoric age ! So I don't have a choice anymore, my knowledge is far too little right now. Thanks for all the help you gave.
  10. I understand what you're saying that the function has far too many responsibilities. My goal was, a function that execute any sql query. Simple. the function opens the DB, execute the query and close the DB, than returns the result (if there's any) . That's it. No fancy stuff here. Just making it easy. I've researched "dependency injection" and "Singleton wrapper". Everytime I found the sql query included in the function. So for every other (different) query I would need another function. That's crazy. And it does somewhat the same as my function, only it's object oriented ( to difficult for me at this time, I'm new into PHP and did ASP before) and you got multiple functions and much more code. So I don't get that. Is it more safe ? I don't think so. If my function would have an problem someday, nothing works, no update/insert/delete... I understand this. I should better make a function for each action. And I'll do this I think. Because I'm new into PHP the function only works with a global statement. Don't ask me why. I don't know why. That's why I used global. But I tried your code but know really how to use the code within the function. For example call_user_func_array(array($stmt, 'bind_param'), $parameters); should come where ? if ($stmt = $mysqli->prepare($sql)) { $stmt->bind_param($parameters); // replace this ? $stmt->execute(); $stmt->close(); }
  11. Ok, I had tested it out with a script on one page, no function at all. There was NO double insert. conclusion... there had to be an error in my codes. In short, I found the error and it works fine now. I did a double query execution in the function while I thought it was just an error checking if the query was good or not. Thanks for the help.
  12. In this example there's no function but I will need to use it with a function and put : "iiisis", $user_id, 1, 0, $lang, 1, $newLink throught the function into $stmt->bind_param(...) that's in the function. The function is to make connection with the database and gives back data from a table or does an update/delete/insert data. The function is : function ShowSaveDelIntoDB($sql, $action, $parameters){ global $db_host; global $db_user; global $db_pass; global $db_name; $ResultShow =''; $mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name); if (mysqli_connect_errno()) { return("No connection with database : %s\n" . mysqli_connect_error()); exit(); } if(isset($action) && $action == "Show" ){ $ResultShow = $mysqli->query($sql); if(!$ResultShow){ return "A problem has occured : " .mysqli_report(MYSQLI_REPORT_ERROR).""; } else { return $ResultShow; } } else{ if ($stmt = $mysqli->prepare($sql)) { $stmt->bind_param($parameters); $stmt->execute(); //return $mysqli->insert_id; $stmt->close(); } } $mysqli->close(); } I hope that now it is more uderstandable. I've looked at call_user_func_array() at http://be.php.net/call_user_func_array and don't understand much of it. $sql = "INSERT tbl_link(userid,linkcat,linksubid,linklang,linkactive,linktitle) VALUES(?, ?, ?, ?, ?, ?)"; $parameters = '"iiisis", $userid, 1, 0, $lang, 1, $newLink'; call_user_func_array(array(ShowSaveDelIntoDB, '__construct'), array($sql,"",$parameters)); gives me plenty errors... If it don't work, I'll work with mysql again because it works very well, -because I don't have the problem with $parameters ! But the codes to secure the input into the database is so ... :'( That's mysql ! That's why I try mysqli. The problem is $parameters.
  13. But the $stmt->bind_param(...) is a part of the function so I can't use this outside the function. Is there any code that makes it as one string so it can be passed along to the function ?
  14. Ok I am trying to use mysqli instead of the usual mysql. Mysql would be outdated. With mysqli, sgl-injection is impossible if you use the "?" in those codes. I would normally use a function but I've made a simple script to find the error. I use $parameters and $sql because these are the data I need to give as parameters to the function, so I used it here too but without the function actually. ini_set('display_errors',1); // 1 == aan , 0 == uit error_reporting(E_ALL | E_STRICT); # sql debug define('DEBUG_MODE',true); // true == aan, false == uit $userid = 11; $lang = 1; $newLink = "testing123"; $db_host = "localhost"; $db_gebruiker = "root"; $db_wachtwoord = ''; $db_naam = "projecteasywebsite"; $sql= "INSERT tbl_link(userid,linkcat,linksubid,linklang,linkactive,linktitle) VALUES(?, ?, ?, ?, ?, ?)"; $parameters = '"iiisis", $userid, 1, 0, $lang, 1, $newLink'; echo $parameters; $mysqli = new mysqli($db_host, $db_gebruiker, $db_wachtwoord, $db_naam); $stmt = $mysqli->prepare($sql); $stmt->bind_param($parameters); $stmt->execute(); echo "<br><br>". mysqli_connect_errno(); echo "<br><br>". mysqli_report(MYSQLI_REPORT_ERROR); $stmt->close(); $mysqli->close(); I got Wrong parameter count for mysqli_stmt::bind_param() So naturally a problem when we execute : Warning: mysqli_stmt::execute() [mysqli-stmt.execute]: (HY000/2031): No data supplied for parameters in prepared statement ($stmt->execute() Is someone using mysqli too ?
  15. Well be damn..d ! PHP has seem to have so many functions, I didn't thought to take a look at those functions ! Now I know it's in my own interest ! Thank you PFMaBiSmAd ! -> http://be2.php.net/manual/en/book.array.php I'll use this site quiet often now !
  16. Perhaps a simple task... I did ASP coding before, now I'm into PHP. So I'm a newbie in PHP. 2 arrays : $array1 = array("1","2","3","4"); and $array2 = array("1","2"); In array2 you'll find the numbers that need to be excluded from array1. So I should have als result 3 and 4. I tried since 2 days now and can only check and exclude 1 number from array2 butnot dynamical ! $ii = 1; foreach( $arrayLordervalue as $value ) { if( $value == 1 )continue; //komma's tussen de cijfers plaatsen if($ii == 1){ $values[$ii] = $value;} else { $values[$ii] = ",". $value; } $value2= $value2.$values[$ii]; $ii++; } echo $value2; Gives me 2,3,4. I'm completely stuck ! I tried foreach, for ($n...),while... bah what's left ?
  17. I've done your suggestion and changed all the includes to include_once but nope, same result. Because the test with a simple insert (only a few line on 1 page) gave me only one insert, I know that I have to search within the codes. At least now I know where to look.
  18. Allright, I got some trail to follow now... A simple test gives me only 1 inserted row !!! $db_host = "localhost"; $db_gebruiker = "root"; $db_wachtwoord = ''; $db_naam = "projecteasywebsite"; $Opdracht = "INSERT INTO tbl_link(userid,linkcat,linksubid,linklang,linkactive,linktitle) VALUES('11',1,0,'1',1,'DoubleInputTest5')"; $Conn = mysql_connect($db_host, $db_gebruiker, $db_wachtwoord); mysql_select_db($db_naam); mysql_query($Opdracht); mysql_close($Conn); The pages where I have the double inserts are setup like this : -------------------------------------------------------------------------------- index.php : included page : $db_host en other DB data & included other page with script to follow --------------------------------------------------------------------------------- -> on execute, new data is send back to index.php but with a paramater that says act=upd (action = update) if($act == "show" ) { include 'menu_show.php'; } if($act == "order") { include 'menu_order.php'; } if($act == "upd") { include 'menu_upd.php'; } ... So the result is this : index.php : included page : $db_host en other DB data it chooses : if($act == "upd") { include 'menu_upd.php'; } -> page menu_upd.php is included now where the new data will be saved in the DB. QUESTION : can these includes be at the root of a double inserts in my case ?
  19. I got that one wrong but even with session_destroy removed double rows are inserted. I have trouble with this one, I see the same answer all over the internet and I am eliminating each possibility with no results.
  20. I've put everything on one page now for testing... I got the same result. A double insert. I even got a session check and it is inserted just 1 time but saved 2 times in mysql db. I got no debug add ons installed infirefox. I tried it with I.E. 8 and got the same result back, debug add on or other also nothing installed on I.E.. The code for the script above, below the database details. Can someone find a clue ? <?php include('includes/dsn_start.php'); session_start(); if(isset($_SESSION['itel'])){ $_SESSION['itel'] = $_SESSION['itel']+ 1; } else { $_SESSION['itel'] = 1; } echo "<br>session: ". $_SESSION['itel']; if(isset($_GET['edit'])){ $edit = htmlspecialchars($_GET['edit']); } if(isset($_GET['act'])){ $act = htmlspecialchars($_GET['act']); } if(isset($_GET['linkid'])){ $linkid = htmlspecialchars($_GET['linkid']); } if(isset($_GET['linksubid'])){ $linksubid = htmlspecialchars($_GET['linksubid']); } if(isset($_GET['userid'])){ $userid = htmlspecialchars($_GET['userid']); //echo "<br><br>xxxxxxx ". $edit ." xxxxx<br><br>"; } if(isset($_POST['newML'])){ $newML = htmlspecialchars($_POST['newML']); function ShowSaveDelIntoDB($Opdracht,$action){ global $db_host; global $db_gebruiker; global $db_wachtwoord; global $db_naam; $ResultShow =''; $Verbinding = mysql_connect($db_host, $db_gebruiker, $db_wachtwoord); # Check of een verbinding is gelukt met de database if (!($verbinding = @ mysql_connect($db_host,$db_gebruiker,$db_wachtwoord))) { trigger_error(mysql_error().'<br />connectie met mysql: '.$db_host); echo "Kan niet connecteren met database."; } else{ mysql_select_db($db_naam); # Check of query is gelukt if (($result_get_user = mysql_query($Opdracht)) === false) { trigger_error(mysql_error().'<br />In query: '.$Opdracht); echo "<br>FOUT in query !<br>"; } else{ # query is gelukt en wordt hier opgehaald if (isset($action) && $action == "Show"){ return $ResultShow = mysql_query($Opdracht); } else{ mysql_query($Opdracht); return mysql_insert_id(); } } mysql_close($Verbinding); } } function orderlink($somevalue){ if(!empty($somevalue)){ $i = 1; $values =""; $value2 =""; foreach($somevalue as $value) { if($i == 1){ $values[$i] = $value;} else { $values[$i] = ",". $value; } $value2= $value2.$values[$i]; $i++; } return $value2; } } if(isset($_GET['act'])){ if($act == "upd"){ $orderlink = orderlink($_POST['orderL']); if($edit == "ML"){ if($_SESSION['itel'] = 1){ $Opdracht = "INSERT INTO tbl_link(userid,linkcat,linksubid,linklang,linkactive,linktitle) VALUES('11',1,0,'1',1,'$newML') ON DUPLICATE KEY UPDATE linkid=LAST_INSERT_ID(linkid), linktitle= '$newML'"; $newlinkid = ShowSaveDelIntoDB($Opdracht,""); //save in DB + give me back the new last inserted rowid } // end if session } } } } session_destroy(); ?> <html> <head> </head> <body> <form name="mainform" action="test.php?edit=ML&act=upd&linkid=1&linksubid=&userid=11" method="POST"><br> <div id="contentLeft"><ul class="ui-sortable"> <br /><li id="recordsArray_4"><input type="hidden" name="orderL[]" value="2">Hoofd1</li> <br /><li id="recordsArray_4"><input type="hidden" name="orderL[]" value="3">Hoodf2</li> <br /><li id="recordsArray_4"><input type="hidden" name="orderL[]" value="4">Hoofd3</li> </ul></div><input type="text" size="30" name="newML" /> <br /><br /> <input type="submit" value="Bewaar" > </form> </body> </html> And this is the table in the database SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+01:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; DROP TABLE IF EXISTS `tbl_link`; CREATE TABLE IF NOT EXISTS `tbl_link` ( `linkid` int(11) NOT NULL AUTO_INCREMENT, `userid` int(11) NOT NULL DEFAULT '0', `linkcat` tinyint(1) NOT NULL DEFAULT '0', `linksubid` tinyint(1) DEFAULT '0', `linksuborder` varchar(25) DEFAULT NULL, `linklang` tinyint(1) NOT NULL DEFAULT '0', `linkactive` tinyint(1) DEFAULT '0', `linktitle` varchar(50) NOT NULL, `articleid` int(11) DEFAULT '0', KEY `linkid` (`linkid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=72 ; INSERT INTO `tbl_link` (`linkid`, `userid`, `linkcat`, `linksubid`, `linksuborder`, `linklang`, `linkactive`, `linktitle`, `articleid`) VALUES (1, 11, 0, 0, '2,3,4', 1, 1, '', 0), (2, 11, 1, 0, '7,8,9', 1, 1, 'Hoofd1', 0), (3, 11, 1, 0, '10', 1, 1, 'Hoodf2', 0), (4, 11, 1, 0, '', 1, 1, 'Hoofd3', 5), (5, 11, 1, 0, '12,11', 2, 1, 'TĂȘte1', 0), (6, 11, 1, 0, '', 2, 1, 'TĂȘte2', , (7, 11, 2, 2, '', 1, 1, 'Subnl1a', 1), (8, 11, 2, 2, '', 1, 1, 'Subnl1b', 2), (9, 11, 2, 2, '', 1, 1, 'Subnl1c', 3), (10, 11, 2, 3, '', 1, 1, 'Subnl2a', 4), (11, 11, 2, 5, '', 2, 1, 'Subfr1a', 6), (12, 11, 2, 5, '', 2, 1, 'Subfr1b', 7), (13, 11, 0, 0, '6,5', 2, 1, '', 0); /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
  21. I have search the net and at the end tried 2 things that didn't solved the problem. It is known that certain browsers can refresh th epage 2 times without us knowing bacause it's doing it all by himself and so fast we don't even see it blink ! So I have following code for the normal sql-insert : $Opdracht = "INSERT INTO tbl_link(userid,linkcat,linksubid,linklang,linkactive,linktitle) VALUES('$userid',1,0,'$lang',1,'$newML')"; it was inserted 2 times... I did some session check : at the top of the page : session_start(); if(isset($_SESSION['itel'])){ $_SESSION['itel'] = $_SESSION['itel']+ 1; } else { $_SESSION['itel'] = 1; } echo "<br>session: ". $_SESSION['itel']; And it gave me number 2 ! This means the page was loaded 2 times, thus inserted 2 times. ! Than I tried : $Opdracht = "INSERT INTO tbl_link(userid,linkcat,linksubid,linklang,linkactive,linktitle) VALUES('$userid',1,0,'$lang',1,'$newML') ON DUPLICATE KEY UPDATE linkid=LAST_INSERT_ID(linkid), linktitle='$linktitle'"; I got no error back but again 2 rows were created instead of 1... These are the fields in the table tbl_link : linkid userid linkcat linksubid linksuborder linklang linkactive linktitle articleid Unfortunately certain fields may be double in multiple rows, the only unique key is "linkid" and that's AUTO_INCREMENT. The only thing I can use is that userid and linktitle may NOT be reproduced 2 times (inserted) !!!
  22. Oh yes indeed, I overlooked this one ! I do need to use $Row[index] system for a function I 'm working on so I can use the same function for different perposes. The example was just a little example. I need only 1 row back in fact. So yes mysql_fetch_row works fine, thanks.
  23. $Row[2] doesn't work but if I use $Row["linkcat"] than it works fine. Is there somewhere to activate in the php.ini file ? // I got 9 fields to in the table tbl_link ! $Row[2] should give me a number. $Verbinding = mysql_connect($db_host, $db_user, $db_passw); mysql_select_db($db_name); $sql = "Select * from tbl_link where userid=1"; $ResultShow = mysql_query($sql); while ($Row = mysql_fetch_assoc($ResultShow)){ echo $Row[2]; }
  24. ALLRIGHT !! I found the answer myself ! And I want to share it with you ! Never knew this. Why the variable with the result from the query didn't wanted to do a second time the loop : A MySQL result resource has an internal pointer, much like a PHP array, and when you have run through it once, the pointer is at the end. You can reset the data pointer using mysql_data_seek(): THUS : mysql_data_seek($query, 0); while ($row = mysql_fetch_array($query)) { Problem solved ! Sorry guys but nobody has tried here. I found the answer at http://stackoverflow.com/questions/9698944/whilerow-mysql-fetch-arrayquery-doesnt-work-in-second-time. They deserve the credits !
×
×
  • 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.